Skip to content

Commit c27ca7b

Browse files
test(javascript): compare thrown messages and values in the behavior relation (#22735)
* test(javascript): compare thrown messages and values in the behavior relation * test(javascript): keep an unreadable thrown object inside the behavior transcript
1 parent e12e122 commit c27ca7b

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

‎test/unitCases/CompareJsTools.unittest.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,18 @@ describe("the behavior relation", () => {
6060
/** @type {import("../../tooling/compare-tools-harness").Report[]} */ (
6161
behaviorDifference("var a;", "throw 1;")
6262
)[0].repro
63-
).toContain("threw <number>");
63+
).toContain("threw <number> 1");
64+
});
65+
66+
it("should report a printed script throwing another message or value", () => {
67+
expect(
68+
behaviorDifference('throw new Error("a");', 'throw new Error("b");')
69+
).toHaveLength(1);
70+
expect(behaviorDifference("throw 1;", "throw 2;")).toHaveLength(1);
71+
expect(behaviorDifference("throw {};", "throw {};")).toEqual([]);
72+
const unreadable =
73+
'throw { get message() { throw new Error("getter"); } };';
74+
expect(behaviorDifference(unreadable, unreadable)).toEqual([]);
6475
});
6576

6677
it("should find nothing where the printed script runs the same", () => {

‎tooling/compare-js-tools.js‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,8 +1753,9 @@ const BEHAVIOR_PRELUDE =
17531753
"Date.now = () => 0;";
17541754

17551755
/**
1756-
* What a script prints and, where it throws, the kind of error, run in a fresh
1757-
* context: a function prints as `[function]`, as its text is the printer's.
1756+
* What a script prints and, where it throws, the error's kind and message or the
1757+
* thrown value, run in a fresh context: a function prints as `[function]`, as
1758+
* its text is the printer's.
17581759
* @param {string} code the script
17591760
* @returns {string | null} its transcript, null where it timed out
17601761
*/
@@ -1777,12 +1778,24 @@ const behaviorOf = (code) => {
17771778
try {
17781779
vm.runInContext(code, context, { timeout: BEHAVIOR_TIMEOUT });
17791780
} catch (error) {
1780-
const { code: reason, name } =
1781-
/** @type {{ code?: unknown, name?: unknown }} */ (
1782-
error !== null && typeof error === "object" ? error : {}
1781+
try {
1782+
const isObject = error !== null && typeof error === "object";
1783+
const {
1784+
code: reason,
1785+
name,
1786+
message
1787+
} = /** @type {{ code?: unknown, name?: unknown, message?: unknown }} */ (
1788+
isObject ? error : {}
17831789
);
1784-
if (reason === "ERR_SCRIPT_EXECUTION_TIMEOUT") return null;
1785-
lines.push(`threw <${typeof name === "string" ? name : typeof error}>`);
1790+
if (reason === "ERR_SCRIPT_EXECUTION_TIMEOUT") return null;
1791+
const kind = typeof name === "string" ? name : typeof error;
1792+
const value =
1793+
typeof message === "string" ? message : isObject ? "" : show(error);
1794+
lines.push(`threw <${kind}>${value ? ` ${value}` : ""}`);
1795+
} catch (_error) {
1796+
// A thrown object whose own properties throw when read.
1797+
lines.push("threw <unreadable>");
1798+
}
17861799
}
17871800
return lines.join("\n");
17881801
};

0 commit comments

Comments
 (0)