Skip to content

Commit 0cdc877

Browse files
committed
assert: show proper differences
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. PR-URL: #18611 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 656a5d0 commit 0cdc877

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

lib/internal/errors.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ function createErrDiff(actual, expected, operator) {
164164
var skipped = false;
165165
const util = lazyUtil();
166166
const actualLines = util
167-
.inspect(actual, { compact: false }).split('\n');
167+
.inspect(actual, { compact: false, customInspect: false }).split('\n');
168168
const expectedLines = util
169-
.inspect(expected, { compact: false }).split('\n');
169+
.inspect(expected, { compact: false, customInspect: false }).split('\n');
170170
const msg = `Input A expected to ${operator} input B:\n` +
171171
`${green}+ expected${white} ${red}- actual${white}`;
172172
const skippedMsg = ' ... Lines skipped';
@@ -310,8 +310,10 @@ class AssertionError extends Error {
310310
} else if (errorDiff === 1) {
311311
// In case the objects are equal but the operator requires unequal, show
312312
// the first object and say A equals B
313-
const res = util
314-
.inspect(actual, { compact: false }).split('\n');
313+
const res = util.inspect(
314+
actual,
315+
{ compact: false, customInspect: false }
316+
).split('\n');
315317

316318
if (res.length > 20) {
317319
res[19] = '...';

test/parallel/test-assert.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,11 @@ common.expectsError(
574574
message: `${start}\n` +
575575
`${actExp}\n` +
576576
'\n' +
577-
' {}'
577+
`${minus} {}\n` +
578+
`${plus} {\n` +
579+
`${plus} loop: 'forever',\n` +
580+
`${plus} [Symbol(util.inspect.custom)]: [Function]\n` +
581+
`${plus} }`
578582
});
579583

580584
// notDeepEqual tests

0 commit comments

Comments
 (0)