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 9fcde68bd5ae15..b6b744c9b1e3ae 100644 --- a/test/parallel/test-assert-async.js +++ b/test/parallel/test-assert-async.js @@ -9,58 +9,65 @@ const wait = promisify(setTimeout); // Test assert.rejects() and assert.doesNotReject() by checking their // expected output and by verifying that they do not work sync -assert.rejects( - () => assert.fail(), - common.expectsError({ - code: 'ERR_ASSERTION', - type: assert.AssertionError, - message: 'Failed' - }) -); +common.crashOnUnhandledRejection(); -assert.doesNotReject(() => {}); +(async () => { + await assert.rejects( + async () => assert.fail(), + common.expectsError({ + code: 'ERR_ASSERTION', + type: assert.AssertionError, + message: 'Failed' + }) + ); -{ - const promise = assert.rejects(async () => { - await wait(1); - assert.fail(); - }, common.expectsError({ - code: 'ERR_ASSERTION', - type: assert.AssertionError, - message: 'Failed' - })); - assert.doesNotReject(() => promise); -} + await assert.doesNotReject(() => {}); -{ - const promise = assert.doesNotReject(async () => { - await wait(1); - throw new Error(); - }); - assert.rejects(() => promise, - (err) => { - assert(err instanceof assert.AssertionError, - `${err.name} is not instance of AssertionError`); - assert.strictEqual(err.code, 'ERR_ASSERTION'); - assert(/^Got unwanted rejection\.\n$/.test(err.message)); - assert.strictEqual(err.operator, 'doesNotReject'); - assert.ok(!err.stack.includes('at Function.doesNotReject')); - return true; - } - ); -} + { + const promise = assert.doesNotReject(async () => { + await wait(1); + throw new Error(); + }); + await assert.rejects( + () => promise, + (err) => { + assert(err instanceof assert.AssertionError, + `${err.name} is not instance of AssertionError`); + assert.strictEqual(err.code, 'ERR_ASSERTION'); + assert(/^Got unwanted rejection\.\n$/.test(err.message)); + assert.strictEqual(err.operator, 'doesNotReject'); + assert.ok(!err.stack.includes('at Function.doesNotReject')); + return true; + } + ); + } -{ - const promise = assert.rejects(() => {}); - assert.rejects(() => promise, - (err) => { - assert(err instanceof assert.AssertionError, - `${err.name} is not instance of AssertionError`); - assert.strictEqual(err.code, 'ERR_ASSERTION'); - assert(/^Missing expected rejection\.$/.test(err.message)); - assert.strictEqual(err.operator, 'rejects'); - assert.ok(!err.stack.includes('at Function.rejects')); - return true; - } - ); -} + { + const promise = assert.rejects(() => {}); + await assert.rejects( + () => promise, + (err) => { + assert(err instanceof assert.AssertionError, + `${err.name} is not instance of AssertionError`); + assert.strictEqual(err.code, 'ERR_ASSERTION'); + assert(/^Missing expected rejection\.$/.test(err.message)); + assert.strictEqual(err.operator, 'rejects'); + assert.ok(!err.stack.includes('at Function.rejects')); + return true; + } + ); + } + + { + 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());