Skip to content

Commit 5e6b42e

Browse files
BridgeARMylesBorins
authored andcommitted
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. 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]>
1 parent 9abbb6b commit 5e6b42e

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
@@ -90,9 +90,9 @@ function createErrDiff(actual, expected, operator){
9090
varskipped=false;
9191
constutil=lazyUtil();
9292
constactualLines=util
93-
.inspect(actual,{compact: false}).split('\n');
93+
.inspect(actual,{compact: false,customInspect: false}).split('\n');
9494
constexpectedLines=util
95-
.inspect(expected,{compact: false}).split('\n');
95+
.inspect(expected,{compact: false,customInspect: false}).split('\n');
9696
constmsg=`Input A expected to ${operator} input B:\n`+
9797
`${green}+ expected${white}${red}- actual${white}`;
9898
constskippedMsg=' ... Lines skipped';
@@ -235,8 +235,10 @@ class AssertionError extends Error{
235235
}elseif(errorDiff===1){
236236
// In case the objects are equal but the operator requires unequal, show
237237
// the first object and say A equals B
238-
constres=util
239-
.inspect(actual,{compact: false}).split('\n');
238+
constres=util.inspect(
239+
actual,
240+
{compact: false,customInspect: false}
241+
).split('\n');
240242

241243
if(res.length>20){
242244
res[19]='...';

‎test/parallel/test-assert.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,11 @@ common.expectsError(
899899
message: `${start}\n`+
900900
`${actExp}\n`+
901901
'\n'+
902-
'{}'
902+
`${minus}{}\n`+
903+
`${plus}{\n`+
904+
`${plus} loop: 'forever',\n`+
905+
`${plus} [Symbol(util.inspect.custom)]: [Function]\n`+
906+
`${plus} }`
903907
});
904908

905909
// notDeepEqual tests

0 commit comments

Comments
(0)