Files
scriptc/tests/corpus/8160-assert-expected-from-fields.cjs
T
Chris Tate 10e3476a55 Convert checked-dynamic expected errors in assert.throws and assert.match
An expected regex or string read from a JavaScript object literal's
field is a checked-dynamic value while its checker type is RegExp or
string. assert.throws chose its regex comparison from the checker type
and passed the value unconverted, an internal compiler error, and
assert.match and the expected-object form refused it. Such a value now
converts to the regex or string its type names.
2026-10-23 21:12:15 -05:00

59 lines
1.5 KiB
JavaScript

// Expected errors stored in an object literal's fields: a JavaScript
// object keeps its fields checked-dynamic, and assert.throws, assert.match
// and the expected-object form convert them to the regex or string the
// fields hold.
"use strict";
const assert = require("assert");
const errMessages = {
auth: / auth/,
state: / state/,
length: /Invalid initialization vector/,
code: "ERR_CRYPTO_INVALID_IV",
};
const error = (message, code) => {
const e = new Error(message);
if (code) e.code = code;
return e;
};
assert.throws(() => {
throw error("Unsupported state or unable to authenticate data");
}, errMessages.auth);
assert.throws(() => {
throw error("Invalid state for operation getAuthTag");
}, errMessages.state);
assert.throws(
() => {
throw error("Invalid initialization vector", "ERR_CRYPTO_INVALID_IV");
},
{ code: errMessages.code, message: errMessages.length },
);
assert.match("unable to auth", errMessages.auth);
assert.doesNotMatch("no match here", errMessages.state);
const report = (label, run) => {
try {
run();
console.log(label, "passed");
} catch (error) {
console.log(label, error.name, error.code, error.message.split("\n")[0]);
}
};
report("wrong message", () =>
assert.throws(() => {
throw error("something else");
}, errMessages.length),
);
report("wrong code", () =>
assert.throws(
() => {
throw error("Invalid initialization vector", "ERR_OTHER");
},
{ code: errMessages.code },
),
);
report("match", () => assert.match("plain text", errMessages.auth));
console.log("done");