Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert: refactor AssertionError properties #25250

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/internal/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,14 @@ class AssertionError extends Error {
this.operator = operator;
Error.captureStackTrace(this, stackStartFn);
}

[inspect.custom](recurseTimes, ctx) {
// This limits the `actual` and `expected` property default inspection to
// the minimum depth. Otherwise those values would be to verbose compared
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
// to the actual error message which contains a combined view of these two
// input values.
return inspect(this, { ...ctx, customInspect: false, depth: 0 });
}
}

module.exports = {
Expand Down
31 changes: 20 additions & 11 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,14 @@ const circular = { y: 1 };
circular.x = circular;

function testAssertionMessage(actual, expected, msg) {
try {
assert.strictEqual(actual, '');
} catch (e) {
assert.strictEqual(
e.message,
msg || strictEqualMessageStart +
`+ actual - expected\n\n+ ${expected}\n- ''`
);
assert.ok(e.generatedMessage, 'Message not marked as generated');
}
assert.throws(
() => assert.strictEqual(actual, ''),
{
generatedMessage: true,
message: msg || strictEqualMessageStart +
`+ actual - expected\n\n+ ${expected}\n- ''`
}
);
}

function testShortAssertionMessage(actual, expected) {
Expand All @@ -280,7 +278,7 @@ testShortAssertionMessage(false, 'false');
testShortAssertionMessage(100, '100');
testShortAssertionMessage(NaN, 'NaN');
testShortAssertionMessage(Infinity, 'Infinity');
testShortAssertionMessage('', '""');
testShortAssertionMessage('a', '"a"');
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
testShortAssertionMessage('foo', '\'foo\'');
testShortAssertionMessage(0, '0');
testShortAssertionMessage(Symbol(), 'Symbol()');
Expand Down Expand Up @@ -1139,3 +1137,14 @@ assert.throws(
'{\n a: true\n}\n'
}
);

{
let threw = false;
try {
assert.deepStrictEqual(Array(100).fill(1), 'foobar');
} catch (err) {
threw = true;
assert(/actual: \[Array],\n expected: 'foobar',/.test(inspect(err)));
}
assert(threw);
}