Skip to content

Commit c53db1e

Browse files
krydosrefack
authored andcommitted
assert: show thrown message in doesNotThrow()
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]>
1 parent 2a621d4 commit c53db1e

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/assert.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,10 @@ function innerThrows(shouldThrow, block, expected, message) {
546546
} else if (actual !== undefined) {
547547
if (!expected || expectedException(actual, expected)) {
548548
details = message ? `: ${message}` : '.';
549-
fail(actual, expected, `Got unwanted exception${details}`, fail);
549+
fail(actual,
550+
expected,
551+
`Got unwanted exception${details}\n${actual.message}`,
552+
fail);
550553
}
551554
throw actual;
552555
}

test/parallel/test-assert.js

+28
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,34 @@ assert.throws(() => {
468468
}, /Got unwanted exception: user message/,
469469
'a.doesNotThrow ignores user message');
470470

471+
{
472+
let threw = false;
473+
try {
474+
assert.doesNotThrow(makeBlock(thrower, Error), 'user message');
475+
} catch (e) {
476+
threw = true;
477+
common.expectsError({
478+
code: 'ERR_ASSERTION',
479+
message: /Got unwanted exception: user message\n\[object Object\]/
480+
})(e);
481+
}
482+
assert.ok(threw);
483+
}
484+
485+
{
486+
let threw = false;
487+
try {
488+
assert.doesNotThrow(makeBlock(thrower, Error));
489+
} catch (e) {
490+
threw = true;
491+
common.expectsError({
492+
code: 'ERR_ASSERTION',
493+
message: /Got unwanted exception\.\n\[object Object\]/
494+
})(e);
495+
}
496+
assert.ok(threw);
497+
}
498+
471499
// make sure that validating using constructor really works
472500
{
473501
let threw = false;

0 commit comments

Comments
 (0)