Files
SnowLuma/.pnpm-store/v11/files/e4/b1de669628b2ee39dc044a4a6fde2480226890aaf18ee6013f500b7331ce29176c5df73c494bfb6102276c98d89534573532ce97757eb96bd250aedf1ceef7
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

55 lines
1.5 KiB
Plaintext

import { expect, test } from "vitest";
import { util } from "zod/v4/core";
test("cached defers the getter until the first read", () => {
let runs = 0;
const box = util.cached(() => ++runs);
expect(runs).toBe(0);
expect(box.value).toBe(1);
expect(box.value).toBe(1);
expect(runs).toBe(1);
});
test("cached memoizes an undefined result", () => {
let runs = 0;
const box = util.cached(() => {
runs++;
return undefined;
});
expect(box.value).toBeUndefined();
expect(box.value).toBeUndefined();
expect(runs).toBe(1);
});
test("cached retries after the getter throws", () => {
let attempts = 0;
const box = util.cached(() => {
if (++attempts < 2) throw new Error("boom");
return "recovered";
});
expect(() => box.value).toThrow("boom");
expect(box.value).toBe("recovered");
expect(attempts).toBe(2);
});
test("cached rejects assignment to value", () => {
const box = util.cached(() => 1);
expect(() => {
(box as { value: number }).value = 2;
}).toThrow(TypeError);
});
// the getter is inherited, so a box carries only its two state slots and stays out of dictionary mode
test("cached keeps value on the prototype", () => {
const box = util.cached(() => 1);
expect(Object.getOwnPropertyDescriptor(box, "value")).toBeUndefined();
expect("value" in box).toBe(true);
expect(box.value).toBe(1);
});
test("allowsEval reads through the same box", () => {
expect(util.allowsEval.value).toBe(true);
expect(util.allowsEval.value).toBe(true);
});