lib: handle throw undefined in assert.throws()

And make `assert.doesNotThrow()` handle it as well.

Backport-PR-URL: https://github.com/nodejs/node/pull/19230
PR-URL: https://github.com/nodejs/node/pull/18029
Fixes: https://github.com/nodejs/node/issues/18027
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Khaidi Chu <i@2333.moe>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Ben Noordhuis authored and Myles Borins committed 2018-03-20 11:33:35 +00:00
1 parent f96ea47cf5
commit b35eabb837
2 files changed
+18 -3

No files matched your search

+6 -3
View File
@@ -31,6 +31,8 @@ const { inspect } = require('util');
const assert = module.exports = ok;
const NO_EXCEPTION_SENTINEL = {};
// All of the following functions must throw an AssertionError
// when a corresponding condition is not met, with a message that
// may be undefined if not provided. All assertion methods provide
@@ -256,6 +258,7 @@ function getActual(block) {
} catch (e) {
return e;
}
return NO_EXCEPTION_SENTINEL;
}
// Expected to throw an error.
@@ -273,7 +276,7 @@ assert.throws = function throws(block, error, message) {
error = null;
}
if (actual === undefined) {
if (actual === NO_EXCEPTION_SENTINEL) {
let details = '';
if (error && error.name) {
details += ` (${error.name})`;
@@ -294,7 +297,7 @@ assert.throws = function throws(block, error, message) {
assert.doesNotThrow = function doesNotThrow(block, error, message) {
const actual = getActual(block);
if (actual === undefined)
if (actual === NO_EXCEPTION_SENTINEL)
return;
if (typeof error === 'string') {
@@ -308,7 +311,7 @@ assert.doesNotThrow = function doesNotThrow(block, error, message) {
actual,
expected: error,
operator: 'doesNotThrow',
message: `Got unwanted exception${details}\n${actual.message}`,
message: `Got unwanted exception${details}\n${actual && actual.message}`,
stackStartFn: doesNotThrow
});
}
+12
View File
@@ -857,4 +857,16 @@ common.expectsError(
message: "message: expected '', not 'foo'"
}
);
// eslint-disable-next-line no-throw-literal
assert.throws(() => { throw undefined; }, /undefined/);
common.expectsError(
// eslint-disable-next-line no-throw-literal
() => assert.doesNotThrow(() => { throw undefined; }),
{
type: assert.AssertionError,
code: 'ERR_ASSERTION',
message: 'Got unwanted exception.\nundefined'
}
);
}