diff --git a/lib/assert.js b/lib/assert.js index 50ab6b39b161bb..0b256e604eeb0e 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -620,8 +620,13 @@ function expectedException(actual, expected, message, fn) { generatedMessage = true; message = 'The error is expected to be an instance of ' + `"${expected.name}". Received `; + // TODO: Special handle identical names. if (isError(actual)) { - message += `"${actual.name}"`; + const name = actual.constructor && actual.constructor.name; + message += `"${name || actual.name}"`; + if (actual.message) { + message += `\n\nError message:\n\n${actual.message}`; + } } else { message += `"${inspect(actual, { depth: -1 })}"`; } @@ -636,6 +641,10 @@ function expectedException(actual, expected, message, fn) { const name = expected.name ? `"${expected.name}" ` : ''; message = `The ${name}validation function is expected to return` + ` "true". Received ${inspect(res)}`; + + if (isError(actual)) { + message += `\n\nCaught error:\n\n${actual}`; + } } throwError = true; } diff --git a/test/parallel/test-assert-async.js b/test/parallel/test-assert-async.js index 45447d456379e1..cbb4431f1952df 100644 --- a/test/parallel/test-assert-async.js +++ b/test/parallel/test-assert-async.js @@ -73,7 +73,8 @@ const invalidThenableFunc = () => { () => assert.rejects(Promise.reject(err), validate), { message: 'The "validate" validation function is expected to ' + - "return \"true\". Received 'baz'", + "return \"true\". Received 'baz'\n\nCaught error:\n\n" + + 'Error: foobar', code: 'ERR_ASSERTION', actual: err, expected: validate, diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 9c59227a78492e..07cf3746965870 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -133,7 +133,7 @@ assert.throws( name: 'AssertionError', operator: 'throws', message: 'The error is expected to be an instance of "AssertionError". ' + - 'Received "TypeError"' + 'Received "TypeError"\n\nError message:\n\n[object Object]' } ); @@ -255,7 +255,7 @@ a.throws(() => thrower(TypeError), (err) => { assert.strictEqual( err.message, 'The error is expected to be an instance of "ES6Error". ' + - 'Received "Error"' + 'Received "AnotherErrorType"\n\nError message:\n\nfoo' ); assert.strictEqual(err.actual, actual); return true; @@ -1334,7 +1334,8 @@ assert.throws( () => assert.throws(() => { throw err; }, validate), { message: 'The validation function is expected to ' + - `return "true". Received ${inspect(validate())}`, + `return "true". Received ${inspect(validate())}\n\nCaught ` + + `error:\n\n${err}`, code: 'ERR_ASSERTION', actual: err, expected: validate,