Skip to content

Commit

Permalink
assert: show thrown message in doesNotThrow()
Browse files Browse the repository at this point in the history
assert.doesNotThrow() should show actual error message instead
of "Got unwanted exception" which is not really helpful.

PR-URL: #12167
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
  • Loading branch information
krydos authored and refack committed Jul 19, 2017
1 parent 2a621d4 commit c53db1e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,10 @@ function innerThrows(shouldThrow, block, expected, message) {
} else if (actual !== undefined) {
if (!expected || expectedException(actual, expected)) {
details = message ? `: ${message}` : '.';
fail(actual, expected, `Got unwanted exception${details}`, fail);
fail(actual,
expected,
`Got unwanted exception${details}\n${actual.message}`,
fail);
}
throw actual;
}
Expand Down
28 changes: 28 additions & 0 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,34 @@ assert.throws(() => {
}, /Got unwanted exception: user message/,
'a.doesNotThrow ignores user message');

{
let threw = false;
try {
assert.doesNotThrow(makeBlock(thrower, Error), 'user message');
} catch (e) {
threw = true;
common.expectsError({
code: 'ERR_ASSERTION',
message: /Got unwanted exception: user message\n\[object Object\]/
})(e);
}
assert.ok(threw);
}

{
let threw = false;
try {
assert.doesNotThrow(makeBlock(thrower, Error));
} catch (e) {
threw = true;
common.expectsError({
code: 'ERR_ASSERTION',
message: /Got unwanted exception\.\n\[object Object\]/
})(e);
}
assert.ok(threw);
}

// make sure that validating using constructor really works
{
let threw = false;
Expand Down

0 comments on commit c53db1e

Please sign in to comment.