diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 71d3e91365146b..7f72e7a0e57893 100755 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -36,21 +36,26 @@ class AssertionError extends Error { if (typeof options !== 'object' || options === null) { throw new exports.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object'); } - if (options.message) { - super(options.message); + var { actual, expected, message, operator, stackStartFunction } = options; + if (message) { + super(message); } else { + if (actual && actual.stack && actual instanceof Error) + actual = `${actual.name}: ${actual.message}`; + if (expected && expected.stack && expected instanceof Error) + expected = `${expected.name}: ${expected.message}`; if (util === null) util = require('util'); - super(`${util.inspect(options.actual).slice(0, 128)} ` + - `${options.operator} ${util.inspect(options.expected).slice(0, 128)}`); + super(`${util.inspect(actual).slice(0, 128)} ` + + `${operator} ${util.inspect(expected).slice(0, 128)}`); } - this.generatedMessage = !options.message; + this.generatedMessage = !message; this.name = 'AssertionError [ERR_ASSERTION]'; this.code = 'ERR_ASSERTION'; - this.actual = options.actual; - this.expected = options.expected; - this.operator = options.operator; - Error.captureStackTrace(this, options.stackStartFunction); + this.actual = actual; + this.expected = expected; + this.operator = operator; + Error.captureStackTrace(this, stackStartFunction); } } diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index d4cd52c37c3839..743cf05ebdac95 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -743,3 +743,12 @@ assert.throws(() => { })); }); } + +common.expectsError( + () => assert.strictEqual(new Error('foo'), new Error('foobar')), + { + code: 'ERR_ASSERTION', + type: assert.AssertionError, + message: /^'Error: foo' === 'Error: foobar'$/ + } +);