diff --git a/lib/internal/errors.js b/lib/internal/errors.js index a41df135407341..5bebf0f092d9af 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -151,9 +151,9 @@ function createErrDiff(actual, expected, operator) { var skipped = false; const util = lazyUtil(); const actualLines = util - .inspect(actual, { compact: false }).split('\n'); + .inspect(actual, { compact: false, customInspect: false }).split('\n'); const expectedLines = util - .inspect(expected, { compact: false }).split('\n'); + .inspect(expected, { compact: false, customInspect: false }).split('\n'); const msg = `Input A expected to ${operator} input B:\n` + `${green}+ expected${white} ${red}- actual${white}`; const skippedMsg = ' ... Lines skipped'; @@ -171,6 +171,8 @@ function createErrDiff(actual, expected, operator) { } actualLines.pop(); expectedLines.pop(); + if (actualLines.length === 0 || expectedLines.length === 0) + break; a = actualLines[actualLines.length - 1]; b = expectedLines[expectedLines.length - 1]; } @@ -295,8 +297,10 @@ class AssertionError extends Error { } else if (errorDiff === 1) { // In case the objects are equal but the operator requires unequal, show // the first object and say A equals B - const res = util - .inspect(actual, { compact: false }).split('\n'); + const res = util.inspect( + actual, + { compact: false, customInspect: false } + ).split('\n'); if (res.length > 20) { res[19] = '...'; diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index e422acbbfbcddd..1800b5aa035b54 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -31,6 +31,7 @@ const { EOL } = require('os'); const EventEmitter = require('events'); const { errorCache } = require('internal/errors'); const { writeFileSync, unlinkSync } = require('fs'); +const { inspect } = require('util'); const a = assert; function makeBlock(f) { @@ -899,6 +900,21 @@ common.expectsError( () => assert.deepEqual(Array(12).fill(1), Array(12).fill(2)), { message }); + const obj1 = {}; + const obj2 = { loop: 'forever' }; + obj2[inspect.custom] = () => '{}'; + // No infinite loop and no custom inspect. + assert.throws(() => assert.deepEqual(obj1, obj2), { + message: `${start}\n` + + `${actExp}\n` + + '\n' + + `${minus} {}\n` + + `${plus} {\n` + + `${plus} loop: 'forever',\n` + + `${plus} [Symbol(util.inspect.custom)]: [Function]\n` + + `${plus} }` + }); + // notDeepEqual tests message = 'Identical input passed to notDeepStrictEqual:\n[\n 1\n]'; assert.throws(