Browse Source

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: https://github.com/nodejs/node/pull/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>
v6
Ruslan Bekenev 8 years ago
committed by Refael Ackermann
parent
commit
c53db1e8e9
No known key found for this signature in database GPG Key ID: CD704BD80FDDDB64
  1. 5
      lib/assert.js
  2. 28
      test/parallel/test-assert.js

5
lib/assert.js

@ -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;
}

28
test/parallel/test-assert.js

@ -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;

Loading…
Cancel
Save