From 00ed77e8170ffb19e385e716ac194a47065075fd Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 28 Dec 2018 15:36:26 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/25250 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- lib/internal/assert.js | 8 ++++++++ test/parallel/test-assert.js | 11 +++++++++++ 2 files changed, 19 insertions(+) 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..bf2da0b5ddb488 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -1139,3 +1139,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); +}