mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-25 06:24:27 +08:00
Assertion failures throw an assert.AssertionError with Node 24.22's generatedMessage, code, actual, expected, operator and diff properties, in Node's key order. new AssertionError(options) validates its options and builds Node's message from operator, actual and expected. AssertionError type annotations, instanceof and assert.throws(fn, AssertionError) compile. Failure paths record actual, expected and operator before they throw; passing assertions do no extra work. The deep forms lower through a per-type function that returns the verdict, so the operands stay call temporaries. assert.equal and notEqual inspect their operands only when they fail. Values without a checked-dynamic form, and values whose type can be cyclic, record undefined.
53 lines
3.3 KiB
TypeScript
53 lines
3.3 KiB
TypeScript
// new AssertionError(options): Node's validation, the message generated
|
|
// from operator, actual and expected, the properties read at their
|
|
// declared types, instanceof, and assert.throws with the class.
|
|
import assert, { AssertionError } from "node:assert";
|
|
|
|
function describe(label: string, error: AssertionError): void {
|
|
console.log(label, JSON.stringify(error.message));
|
|
console.log(" ", error.name, error.code, error.generatedMessage, error.operator, error.actual, error.expected);
|
|
console.log(" ", error instanceof AssertionError, error instanceof Error);
|
|
}
|
|
|
|
describe("strictEqual", new AssertionError({ actual: 1, expected: 2, operator: "strictEqual" }));
|
|
describe("strictEqual strings", new AssertionError({ actual: "a long string here", expected: "a long string there", operator: "strictEqual" }));
|
|
describe("deepStrictEqual", new AssertionError({ actual: { a: 1 }, expected: { a: 2 }, operator: "deepStrictEqual" }));
|
|
describe("notStrictEqual", new AssertionError({ actual: 5, expected: 5, operator: "notStrictEqual" }));
|
|
describe("notDeepStrictEqual", new AssertionError({ actual: [1, 2], expected: [1, 2], operator: "notDeepStrictEqual" }));
|
|
describe("deepEqual", new AssertionError({ actual: { a: 1 }, expected: { a: 2 }, operator: "deepEqual" }));
|
|
describe("notDeepEqual same", new AssertionError({ actual: { a: 1 }, expected: { a: 1 }, operator: "notDeepEqual" }));
|
|
describe("notDeepEqual different", new AssertionError({ actual: 1, expected: "1", operator: "notDeepEqual" }));
|
|
describe("other operator", new AssertionError({ actual: 1, expected: 2, operator: "<" }));
|
|
// Without an operator the property reads as undefined, which the
|
|
// declared string type does not cover: read it untyped.
|
|
function describeUntyped(label: string, error: any): void {
|
|
console.log(label, JSON.stringify(error.message), error.operator, error.actual, error.expected);
|
|
}
|
|
describeUntyped("no operator", new AssertionError({ actual: 1, expected: 2 }));
|
|
describeUntyped("empty", new AssertionError({}));
|
|
describe("message", new AssertionError({ message: "boom", actual: 1, expected: 2, operator: "==" }));
|
|
describe("message strictEqual", new AssertionError({ message: "mine", actual: 1, expected: 2, operator: "strictEqual" }));
|
|
describe("message empty", new AssertionError({ message: "", actual: 1, expected: 2, operator: "deepEqual" }));
|
|
|
|
const error = new AssertionError({ actual: 1, expected: 2, operator: "strictEqual" });
|
|
console.log(String(error));
|
|
console.log(error.toString() === String(error));
|
|
const fromOptions = new AssertionError({ actual: [1], expected: [2], operator: "deepEqual", diff: "full" });
|
|
console.log(fromOptions.message.split("\n").length);
|
|
|
|
assert.throws(() => assert.strictEqual(1, 2), AssertionError);
|
|
assert.throws(() => { throw new AssertionError({ message: "thrown" }); }, AssertionError);
|
|
assert.throws(() => { throw new AssertionError({ message: "thrown" }); }, { name: "AssertionError", code: "ERR_ASSERTION", message: "thrown" });
|
|
try {
|
|
assert.throws(() => { throw new TypeError("not an assertion"); }, AssertionError);
|
|
} catch (e) {
|
|
console.log((e as Error).message.split("\n")[0]);
|
|
}
|
|
try {
|
|
assert.deepStrictEqual({ list: [1, 2, 3] }, { list: [1, 2, 4] }, "lists differ");
|
|
} catch (e) {
|
|
if (e instanceof AssertionError) {
|
|
console.log(e.generatedMessage, e.operator, JSON.stringify(e.actual), JSON.stringify(e.expected));
|
|
}
|
|
}
|