Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0d241ba

Browse filesBrowse files
not-an-aardvarkMylesBorins
authored andcommitted
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 Backport-PR-URL: #24019 PR-URL: #19650 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 3cd4462 commit 0d241ba
Copy full SHA for 0d241ba

File tree

Expand file treeCollapse file tree

2 files changed

+18
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+18
-2
lines changed
Open diff view settings
Collapse file

‎lib/assert.js‎

Copy file name to clipboardExpand all lines: lib/assert.js
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,11 @@ async function waitForActual(block) {
722722
if (typeof block !== 'function') {
723723
throw new errors.ERR_INVALID_ARG_TYPE('block', 'Function', block);
724724
}
725+
726+
// Return a rejected promise if `block` throws synchronously.
727+
const resultPromise = block();
725728
try {
726-
await block();
729+
await resultPromise;
727730
} catch (e) {
728731
return e;
729732
}
Collapse file

‎test/parallel/test-assert-async.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-assert-async.js
+14-1Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ common.crashOnUnhandledRejection();
1313

1414
(async () => {
1515
await assert.rejects(
16-
() => assert.fail(),
16+
async () => assert.fail('Failed'),
1717
common.expectsError({
1818
code: 'ERR_ASSERTION',
1919
type: assert.AssertionError,
@@ -57,4 +57,17 @@ common.crashOnUnhandledRejection();
5757
}
5858
);
5959
}
60+
61+
{
62+
const THROWN_ERROR = new Error();
63+
64+
await assert.rejects(() => {
65+
throw THROWN_ERROR;
66+
}).then(common.mustNotCall())
67+
.catch(
68+
common.mustCall((err) => {
69+
assert.strictEqual(err, THROWN_ERROR);
70+
})
71+
);
72+
}
6073
})().then(common.mustCall());

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.