diff --git a/src/domain/immutableJson.fixture.ts b/src/domain/immutableJson.fixture.ts new file mode 100644 index 00000000000..c27db3d4e57 --- /dev/null +++ b/src/domain/immutableJson.fixture.ts @@ -0,0 +1,13 @@ +export const proxySnapshotFixture = () => { + let reflected = false; + const proxy = new Proxy( + { value: 1 }, + { + getPrototypeOf() { + reflected = true; + throw new Error("must not reflect on a proxy"); + }, + }, + ); + return { proxy, reflected: () => reflected }; +}; diff --git a/src/domain/immutableJson.test.ts b/src/domain/immutableJson.test.ts index c81a3590f97..548733e7fab 100644 --- a/src/domain/immutableJson.test.ts +++ b/src/domain/immutableJson.test.ts @@ -8,6 +8,7 @@ import { immutableJsonSnapshotMember, isImmutableJsonSnapshot, } from "./immutableJson.js"; +import { proxySnapshotFixture } from "./immutableJson.fixture.js"; describe("owned immutable JSON snapshots", () => { it("never borrows a member that sealing did not visit", () => { @@ -173,23 +174,14 @@ describe("owned immutable JSON snapshots", () => { }); it("rejects proxy snapshots before reflecting on their mutable behavior", () => { - let reflected = false; - const proxy = new Proxy( - { value: 1 }, - { - getPrototypeOf() { - reflected = true; - throw new Error("must not reflect on a proxy"); - }, - }, - ); + const { proxy, reflected } = proxySnapshotFixture(); const root = { child: proxy }; for (const input of [proxy, root]) { expect(() => freezeJsonSnapshot(input)).toThrow("proxies"); expect(isImmutableJsonSnapshot(input)).toBe(false); } expect(Object.isFrozen(root)).toBe(false); - expect(reflected).toBe(false); + expect(reflected()).toBe(false); }); }); @@ -248,20 +240,11 @@ describe("borrowing a parsed JSON snapshot", () => { }); it("declines parser-introduced proxies without reflecting on them", () => { - let reflected = false; - const parsed = new Proxy( - { value: 1 }, - { - getPrototypeOf() { - reflected = true; - throw new Error("must not reflect on a proxy"); - }, - }, - ); + const { proxy: parsed, reflected } = proxySnapshotFixture(); expect( canBorrowParsedJsonSnapshot(freezeJsonSnapshot({ value: 1 }), parsed), ).toBe(false); - expect(reflected).toBe(false); + expect(reflected()).toBe(false); }); it("compares deeply nested snapshots without depending on the call stack", () => { diff --git a/src/domain/immutableJsonSteps.test.ts b/src/domain/immutableJsonSteps.test.ts index 3e8babfb078..a6b032c28c7 100644 --- a/src/domain/immutableJsonSteps.test.ts +++ b/src/domain/immutableJsonSteps.test.ts @@ -4,6 +4,7 @@ import { immutableJsonSnapshotMember, isImmutableJsonSnapshot, } from "./immutableJson.js"; +import { proxySnapshotFixture } from "./immutableJson.fixture.js"; import { jsonValueValidationIssue, type JsonValue } from "./jsonValue.js"; const complete = (steps: Iterator): Value => { @@ -93,16 +94,7 @@ describe("cooperative owned snapshot sealing", () => { }); it("never authenticates roots containing proxies or invokes their traps", () => { - let reflected = false; - const proxy = new Proxy( - { value: 1 }, - { - getPrototypeOf() { - reflected = true; - throw new Error("must not reflect on a proxy"); - }, - }, - ); + const { proxy, reflected } = proxySnapshotFixture(); const root = { child: proxy }; for (const input of [proxy, root]) { expect(() => complete(freezeOwnedJsonSnapshotSteps(input))).toThrow( @@ -111,6 +103,6 @@ describe("cooperative owned snapshot sealing", () => { expect(isImmutableJsonSnapshot(input)).toBe(false); expect(immutableJsonSnapshotMember(input, "child")).toBeUndefined(); } - expect(reflected).toBe(false); + expect(reflected()).toBe(false); }); });