mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-21 05:50:55 +08:00
84 lines
4.1 KiB
JavaScript
84 lines
4.1 KiB
JavaScript
// The island's node:assert shim, differentially against Node: codes,
|
|
// operators, actual/expected, generatedMessage flags, and the simple
|
|
// generated-message forms byte-exact. Rich diff bodies (deep-equality
|
|
// failures) and call-source introspection are documented divergences —
|
|
// those cases pin everything BUT the message text.
|
|
import assert, { AssertionError, strict } from "node:assert";
|
|
import strictDefault from "node:assert/strict";
|
|
|
|
export async function report() {
|
|
const lines = [];
|
|
const shape = async (label, fn, withMessage) => {
|
|
try {
|
|
await fn();
|
|
lines.push(label + " ok");
|
|
} catch (e) {
|
|
lines.push(label + " " + [
|
|
e.code || e.constructor.name,
|
|
e.name,
|
|
"op:" + e.operator,
|
|
"gen:" + e.generatedMessage,
|
|
withMessage === false ? "<msg elided>" : JSON.stringify(e.message),
|
|
].join("|"));
|
|
}
|
|
};
|
|
|
|
await shape("ok-pass", () => assert(1));
|
|
await shape("ok-msg", () => assert(0, "custom message"));
|
|
await shape("ok-err", () => assert.ok(false, new TypeError("boom")));
|
|
await shape("equal-pass", () => assert.equal(1, "1"));
|
|
await shape("equal-fail", () => assert.equal(1, 2, "nope"));
|
|
await shape("equal-nan", () => assert.equal(NaN, NaN));
|
|
await shape("strictEqual-fail", () => assert.strictEqual(1, 2));
|
|
await shape("strictEqual-str", () => assert.strictEqual("a", "b"));
|
|
await shape("notStrictEqual-fail", () => assert.notStrictEqual(3, 3));
|
|
await shape("deepStrictEqual-pass", () => assert.deepStrictEqual({ x: [1] }, { x: [1] }));
|
|
await shape("deepStrictEqual-fail", () => assert.deepStrictEqual({ x: 1 }, { x: 2 }, "deep nope"), false);
|
|
await shape("notDeepStrictEqual-fail", () => assert.notDeepStrictEqual({ x: 1 }, { x: 1 }, "same"));
|
|
await shape("deepEqual-loose-pass", () => assert.deepEqual({ x: "1" }, { x: 1 }));
|
|
await shape("deepEqual-loose-proto", () => assert.deepEqual(Object.assign(Object.create(null), { a: 1 }), { a: 1 }));
|
|
await shape("throws-pass", () => assert.throws(() => {
|
|
throw new TypeError("t");
|
|
}, TypeError));
|
|
await shape("throws-class-fail", () => assert.throws(() => {
|
|
throw new TypeError("t");
|
|
}, RangeError));
|
|
await shape("throws-regex-pass", () => assert.throws(() => {
|
|
throw new Error("has needle inside");
|
|
}, /needle/));
|
|
await shape("throws-regex-fail", () => assert.throws(() => {
|
|
throw new Error("nope");
|
|
}, /needle/));
|
|
await shape("throws-obj-pass", () => assert.throws(() => {
|
|
const e = new Error("m");
|
|
e.code = "X";
|
|
throw e;
|
|
}, { code: "X" }));
|
|
await shape("throws-missing", () => assert.throws(() => {}, "ctx"));
|
|
await shape("doesNotThrow-fail", () => assert.doesNotThrow(() => {
|
|
throw new Error("oops");
|
|
}));
|
|
await shape("rejects-pass", () => assert.rejects(Promise.reject(new TypeError("r")), TypeError));
|
|
await shape("rejects-missing", () => assert.rejects(Promise.resolve(1)));
|
|
await shape("rejects-fn", () => assert.rejects(async () => {
|
|
throw new Error("af");
|
|
}, /af/));
|
|
await shape("match-pass", () => assert.match("abc", /b/));
|
|
await shape("match-fail", () => assert.match("abc", /z/));
|
|
await shape("match-notstr", () => assert.match(5, /z/));
|
|
await shape("match-badre", () => assert.match("abc", "b"));
|
|
await shape("doesNotMatch-fail", () => assert.doesNotMatch("abc", /b/));
|
|
await shape("ifError-null", () => assert.ifError(null));
|
|
await shape("ifError-err", () => assert.ifError(new Error("bad")));
|
|
await shape("ifError-value", () => assert.ifError(42));
|
|
await shape("fail-0", () => assert.fail());
|
|
await shape("fail-msg", () => assert.fail("custom"));
|
|
await shape("fail-err", () => assert.fail(new RangeError("re")));
|
|
await shape("strict-equal", () => strict.equal(1, "1"));
|
|
lines.push("strict-module " + (strictDefault === strict) + " " + (strict.strict === strict));
|
|
const direct = new AssertionError({ message: "direct", actual: 1, expected: 2, operator: "custom" });
|
|
lines.push("direct " + [direct.code, direct.name, direct.message, direct.operator, direct.actual, direct.expected, direct.generatedMessage].join("|"));
|
|
lines.push("instanceof " + (direct instanceof Error));
|
|
return lines.join("\n");
|
|
}
|