test(json): share proxy observation fixtures (#16449)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-03 19:40:49 +00:00
1 parent 524c76396a
commit 71b9f5cc01
3 files changed
+21 -33

No files matched your search

+13
View File
@@ -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 };
};
+5 -22
View File
@@ -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", () => {
+3 -11
View File
@@ -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 = <Value>(steps: Iterator<void, Value>): 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);
});
});