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.
  • Loading branch information
krydos committed Jul 15, 2017
1 parent 016d81c commit d023feb
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 d023feb

Please sign in to comment.