diff --git a/lib/internal/assert.js b/lib/internal/assert.js index 29769fc5617b95..4e059db6b9d478 100644 --- a/lib/internal/assert.js +++ b/lib/internal/assert.js @@ -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 = { diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index db00ed2836327f..a7c37e8fb33024 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -257,16 +257,14 @@ const circular = { y: 1 }; circular.x = circular; function testAssertionMessage(actual, expected, msg) { - try { - assert.strictEqual(actual, ''); - } catch (e) { - assert.strictEqual( - e.message, - msg || strictEqualMessageStart + - `+ actual - expected\n\n+ ${expected}\n- ''` - ); - assert.ok(e.generatedMessage, 'Message not marked as generated'); - } + assert.throws( + () => assert.strictEqual(actual, ''), + { + generatedMessage: true, + message: msg || strictEqualMessageStart + + `+ actual - expected\n\n+ ${expected}\n- ''` + } + ); } function testShortAssertionMessage(actual, expected) { @@ -280,7 +278,7 @@ testShortAssertionMessage(false, 'false'); testShortAssertionMessage(100, '100'); testShortAssertionMessage(NaN, 'NaN'); testShortAssertionMessage(Infinity, 'Infinity'); -testShortAssertionMessage('', '""'); +testShortAssertionMessage('a', '"a"'); testShortAssertionMessage('foo', '\'foo\''); testShortAssertionMessage(0, '0'); testShortAssertionMessage(Symbol(), 'Symbol()'); @@ -1139,3 +1137,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); +}