assert: ensure .rejects() disallows sync throws
This updates `assert.rejects()` to disallow any errors that are thrown synchronously from the given function. Previously, throwing an error would cause the same behavior as returning a rejected Promise. Fixes: https://github.com/nodejs/node/issues/19646 PR-URL: https://github.com/nodejs/node/pull/19650 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
8995125c14
commit
fdb35d8960
@ -451,8 +451,11 @@ async function waitForActual(block) {
|
||||
if (typeof block !== 'function') {
|
||||
throw new ERR_INVALID_ARG_TYPE('block', 'Function', block);
|
||||
}
|
||||
|
||||
// Return a rejected promise if `block` throws synchronously.
|
||||
const resultPromise = block();
|
||||
try {
|
||||
await block();
|
||||
await resultPromise;
|
||||
} catch (e) {
|
||||
return e;
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ common.crashOnUnhandledRejection();
|
||||
|
||||
(async () => {
|
||||
await assert.rejects(
|
||||
() => assert.fail(),
|
||||
async () => assert.fail(),
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
type: assert.AssertionError,
|
||||
@ -57,4 +57,17 @@ common.crashOnUnhandledRejection();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
const THROWN_ERROR = new Error();
|
||||
|
||||
await assert.rejects(() => {
|
||||
throw THROWN_ERROR;
|
||||
}).then(common.mustNotCall())
|
||||
.catch(
|
||||
common.mustCall((err) => {
|
||||
assert.strictEqual(err, THROWN_ERROR);
|
||||
})
|
||||
);
|
||||
}
|
||||
})().then(common.mustCall());
|
||||
|
Loading…
x
Reference in New Issue
Block a user