Skip to content

Commit e09dd0c

Browse files
BridgeARaddaleax
authored andcommitted
assert: make actual and expected getters
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 <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 2e6e4cf commit e09dd0c

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/internal/assert.js

+8
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,14 @@ class AssertionError extends Error {
395395
this.operator = operator;
396396
Error.captureStackTrace(this, stackStartFn);
397397
}
398+
399+
[inspect.custom](recurseTimes, ctx) {
400+
// This limits the `actual` and `expected` property default inspection to
401+
// the minimum depth. Otherwise those values would be too verbose compared
402+
// to the actual error message which contains a combined view of these two
403+
// input values.
404+
return inspect(this, { ...ctx, customInspect: false, depth: 0 });
405+
}
398406
}
399407

400408
module.exports = {

test/parallel/test-assert.js

+11
Original file line numberDiff line numberDiff line change
@@ -1139,3 +1139,14 @@ assert.throws(
11391139
'{\n a: true\n}\n'
11401140
}
11411141
);
1142+
1143+
{
1144+
let threw = false;
1145+
try {
1146+
assert.deepStrictEqual(Array(100).fill(1), 'foobar');
1147+
} catch (err) {
1148+
threw = true;
1149+
assert(/actual: \[Array],\n expected: 'foobar',/.test(inspect(err)));
1150+
}
1151+
assert(threw);
1152+
}

0 commit comments

Comments
 (0)