diff --git a/lib/assert.js b/lib/assert.js index 2fc3cf33e80a18..9c900fcaf32055 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -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; } diff --git a/test/parallel/test-assert-async.js b/test/parallel/test-assert-async.js index ab03a53cd302c7..b6b744c9b1e3ae 100644 --- a/test/parallel/test-assert-async.js +++ b/test/parallel/test-assert-async.js @@ -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());