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: nodejs#12167
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
  • 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.