Skip to content

Commit

Permalink
assert: make actual and expected getters
Browse files Browse the repository at this point in the history
The `actual` and `expected` properties on an instance of
`AssertionError` is now a getter to prevent inspecting these when
inspecting the error. These values will be visible in the error
message and showing them otherwise would decrease the readability
of the error.

PR-URL: #25250
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
BridgeAR authored and addaleax committed Jan 14, 2019
1 parent 2e6e4cf commit e09dd0c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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 too verbose compared
// 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
11 changes: 11 additions & 0 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1139,3 +1139,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);
}

0 comments on commit e09dd0c

Please sign in to comment.