Skip to content
Myr

chore(test): pin draft prototype-inspection behavior restored by #1271 (#1272)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Drafts must stay safe for ecosystem introspection idioms: lodash's isPrototype (used by isEmpty/isEqual/isPlainObject) reads value.constructor.prototype, and fast-deep-equal compares constructor identity. The guard reverted in #1271 broke these (#1265, #1266, #1268) while its tests still passed, because try/catch made a crash and a successful block indistinguishable. These tests assert observable behavior directly, so they fail on 11.1.9-11.1.11 (verified: constructor.prototype throws, constructor identity and 'in' checks break, writes to own keys named prototype are dropped) and pass on main. setPrototypeOf remains forbidden; the applyPatches-layer pollution guards in patch.js are untouched.

authored by Jonatan Kruszewski · committed by GitHub · parent 955c5f5 · Browse files · 1 file+93 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+93 added−0 removed

  • tests:+93 added−0 removed

1 file+93 added−0 removed

added:__tests__/prototype-inspection.js+93 added
1"use strict"2import {produce, immerable} from "../src/immer"34// Ecosystem libraries introspect prototypes to classify values, and reducers5// routinely run them against drafts. The patterns below are lifted from the6// most-installed consumers so drafts stay safe for them:7// - lodash `isPrototype` (used by _.isEmpty, _.isEqual, _.isPlainObject):8// reads `value.constructor.prototype`9// - fast-deep-equal: compares `a.constructor !== b.constructor`10// These once crashed or misreported on drafts (#1265, #1266, #1268) because a11// guard wrapped `constructor` reads in a Proxy, violating the engine invariant12// for Object's non-configurable `prototype` property. The guard was reverted13// in #1271; these tests pin the restored behavior.14function isPrototype(value) {15 const Ctor = value && value.constructor16 const proto =17 (typeof Ctor == "function" && Ctor.prototype) || Object.prototype18 return value === proto19}2021describe("prototype inspection", () => {22 it("returns the real constructor for object and array drafts", () => {23 produce({a: {}, b: []}, draft => {24 expect(draft.constructor).toBe(Object)25 expect(draft.a.constructor).toBe(Object)26 expect(draft.b.constructor).toBe(Array)27 })28 })2930 it("returns the real constructor for class drafts", () => {31 class Todo {32 [immerable] = true33 title = "test"34 }35 produce(new Todo(), draft => {36 expect(draft.constructor).toBe(Todo)37 })38 })3940 it("keeps constructor identity stable across drafts - #1266", () => {41 produce({a: {}, b: {}}, draft => {42 expect(draft.a.constructor).toBe(draft.b.constructor)43 expect(draft.a.constructor !== draft.b.constructor).toBe(false)44 })45 })4647 it("allows reading constructor.prototype - #1265, #1268", () => {48 produce({}, draft => {49 expect(() => draft.constructor.prototype).not.toThrow()50 expect(draft.constructor.prototype).toBe(Object.prototype)51 })52 })5354 it("supports lodash's isPrototype idiom on drafts - #1268", () => {55 produce({items: [{done: false}]}, draft => {56 expect(() => isPrototype(draft)).not.toThrow()57 expect(isPrototype(draft)).toBe(false)58 expect(isPrototype(draft.items)).toBe(false)59 expect(isPrototype(draft.items[0])).toBe(false)60 })61 })6263 it("reports reserved names through `in` and Object.keys", () => {64 produce({a: 1}, draft => {65 expect("constructor" in draft).toBe(true)66 expect(Object.keys(draft)).toEqual(["a"])67 })68 })6970 it("round-trips own data keys named like reserved properties", () => {71 const base = {constructor: "not a function"}72 produce(base, draft => {73 expect(draft.constructor).toBe("not a function")74 })75 const result = produce({}, draft => {76 draft.prototype = 4277 })78 expect(result.prototype).toBe(42)79 })8081 it("returns the real prototype for __proto__ reads", () => {82 produce({}, draft => {83 expect(Object.getPrototypeOf(draft)).toBe(Object.prototype)84 expect(draft.__proto__).toBe(Object.prototype)85 })86 })8788 it("still forbids setPrototypeOf on drafts", () => {89 produce({}, draft => {90 expect(() => Object.setPrototypeOf(draft, {})).toThrow()91 })92 })93})