Skip to content

Commit

Permalink
assert: show proper differences
Browse files Browse the repository at this point in the history
Right now it is possible to get an AssertionError from input that has
the customInspect function set to always return the same value.

That way the error message is actually misleading because the output
is going to look the same. This fixes it by deactivating the custom
inspect function.

Backport-PR-URL: #19230
PR-URL: #18611
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
BridgeAR authored and MylesBorins committed Mar 13, 2018
1 parent 4ca0fc4 commit 61baaba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ function createErrDiff(actual, expected, operator) {
var skipped = false;
const util = lazyUtil();
const actualLines = util
.inspect(actual, { compact: false }).split('\n');
.inspect(actual, { compact: false, customInspect: false }).split('\n');
const expectedLines = util
.inspect(expected, { compact: false }).split('\n');
.inspect(expected, { compact: false, customInspect: false }).split('\n');
const msg = `Input A expected to ${operator} input B:\n` +
`${green}+ expected${white} ${red}- actual${white}`;
const skippedMsg = ' ... Lines skipped';
Expand Down Expand Up @@ -235,8 +235,10 @@ class AssertionError extends Error {
} else if (errorDiff === 1) {
// In case the objects are equal but the operator requires unequal, show
// the first object and say A equals B
const res = util
.inspect(actual, { compact: false }).split('\n');
const res = util.inspect(
actual,
{ compact: false, customInspect: false }
).split('\n');

if (res.length > 20) {
res[19] = '...';
Expand Down
6 changes: 5 additions & 1 deletion test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,11 @@ common.expectsError(
message: `${start}\n` +
`${actExp}\n` +
'\n' +
' {}'
`${minus} {}\n` +
`${plus} {\n` +
`${plus} loop: 'forever',\n` +
`${plus} [Symbol(util.inspect.custom)]: [Function]\n` +
`${plus} }`
});

// notDeepEqual tests
Expand Down

0 comments on commit 61baaba

Please sign in to comment.