Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
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: #19646
  • Loading branch information
not-an-aardvark committed Mar 28, 2018
commit 75d2d1e289c49c82e3eafc9469a8396e853bd91c
5 changes: 4 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 14 additions & 1 deletion test/parallel/test-assert-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ common.crashOnUnhandledRejection();

(async () => {
await assert.rejects(
() => assert.fail(),
async () => assert.fail(),
common.expectsError({
code: 'ERR_ASSERTION',
type: assert.AssertionError,
Expand Down Expand Up @@ -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());