Skip to content

Commit

Permalink
assert: add more information to AssertionErrors
Browse files Browse the repository at this point in the history
This adds information about the actual thrown error to the
AssertionError's message property.

It also improves the logged error instances error name by using the
constructors name, if available.

PR-URL: #28263
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
BridgeAR committed Oct 1, 2019
1 parent 5700cd1 commit 97c52ca
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
11 changes: 10 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })}"`;
}
Expand All @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-assert-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]'
}
);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 97c52ca

Please sign in to comment.