diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2018-04-08 23:28:30 +0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-04-16 03:21:45 +0200 |
commit | 2c3146d06dfc1815915232eae38da3cdeaaaeb13 (patch) | |
tree | dc9fd43615ebfabe44196da49fcf117c4983404e /test/parallel/test-assert-async.js | |
parent | 11819c7773d123efb8db836aefc73bde8c5f431a (diff) | |
download | android-node-v8-2c3146d06dfc1815915232eae38da3cdeaaaeb13.tar.gz android-node-v8-2c3146d06dfc1815915232eae38da3cdeaaaeb13.tar.bz2 android-node-v8-2c3146d06dfc1815915232eae38da3cdeaaaeb13.zip |
assert: add direct promises support in rejects
This adds direct promise support to `assert.rejects` and
`assert.doesNotReject`. It will now accept both, functions and ES2015
promises as input.
Besides this the documentation was updated to reflect the latest
changes.
It also refactors the tests to a non blocking way to improve the
execution performance and improves the coverage.
PR-URL: https://github.com/nodejs/node/pull/19885
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Diffstat (limited to 'test/parallel/test-assert-async.js')
-rw-r--r-- | test/parallel/test-assert-async.js | 156 |
1 files changed, 99 insertions, 57 deletions
diff --git a/test/parallel/test-assert-async.js b/test/parallel/test-assert-async.js index cc557d504a..2a865c121f 100644 --- a/test/parallel/test-assert-async.js +++ b/test/parallel/test-assert-async.js @@ -1,74 +1,116 @@ 'use strict'; const common = require('../common'); const assert = require('assert'); -const { promisify } = require('util'); -const wait = promisify(setTimeout); - -/* eslint-disable prefer-common-expectserror, no-restricted-properties */ // Test assert.rejects() and assert.doesNotReject() by checking their // expected output and by verifying that they do not work sync common.crashOnUnhandledRejection(); -(async () => { - await assert.rejects( - async () => assert.fail(), - common.expectsError({ - code: 'ERR_ASSERTION', - type: assert.AssertionError, - message: 'Failed' - }) - ); +// Run all tests in parallel and check their outcome at the end. +const promises = []; - await assert.doesNotReject(() => {}); +// Check `assert.rejects`. +{ + const rejectingFn = async () => assert.fail(); + const errObj = { + code: 'ERR_ASSERTION', + name: 'AssertionError [ERR_ASSERTION]', + message: 'Failed' + }; + // `assert.rejects` accepts a function or a promise as first argument. + promises.push(assert.rejects(rejectingFn, errObj)); + promises.push(assert.rejects(rejectingFn(), errObj)); +} - { - 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.strictEqual(err.message, - 'Got unwanted rejection.\nActual message: ""'); - assert.strictEqual(err.operator, 'doesNotReject'); - assert.ok(!err.stack.includes('at Function.doesNotReject')); - return true; - } - ); - } +{ + const handler = (err) => { + assert(err instanceof assert.AssertionError, + `${err.name} is not instance of AssertionError`); + assert.strictEqual(err.code, 'ERR_ASSERTION'); + assert.strictEqual(err.message, + 'Missing expected rejection (handler).'); + assert.strictEqual(err.operator, 'rejects'); + assert.ok(!err.stack.includes('at Function.rejects')); + return true; + }; + + let promise = assert.rejects(async () => {}, handler); + promises.push(assert.rejects(promise, handler)); + + promise = assert.rejects(() => {}, handler); + promises.push(assert.rejects(promise, handler)); + + promise = assert.rejects(Promise.resolve(), handler); + promises.push(assert.rejects(promise, handler)); +} + +{ + const THROWN_ERROR = new Error(); + + promises.push(assert.rejects(() => { + throw THROWN_ERROR; + }).catch(common.mustCall((err) => { + assert.strictEqual(err, THROWN_ERROR); + }))); +} +promises.push(assert.rejects( + assert.rejects('fail', {}), { - 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; - } - ); + code: 'ERR_INVALID_ARG_TYPE', + message: 'The "block" argument must be one of type ' + + 'Function or Promise. Received type string' } +)); +// Check `assert.doesNotReject`. +{ + // `assert.doesNotReject` accepts a function or a promise as first argument. + promises.push(assert.doesNotReject(() => {})); + promises.push(assert.doesNotReject(async () => {})); + promises.push(assert.doesNotReject(Promise.resolve())); +} + +{ + const handler1 = (err) => { + assert(err instanceof assert.AssertionError, + `${err.name} is not instance of AssertionError`); + assert.strictEqual(err.code, 'ERR_ASSERTION'); + assert.strictEqual(err.message, 'Failed'); + return true; + }; + const handler2 = (err) => { + assert(err instanceof assert.AssertionError, + `${err.name} is not instance of AssertionError`); + assert.strictEqual(err.code, 'ERR_ASSERTION'); + assert.strictEqual(err.message, + 'Got unwanted rejection.\nActual message: "Failed"'); + assert.strictEqual(err.operator, 'doesNotReject'); + assert.ok(!err.stack.includes('at Function.doesNotReject')); + return true; + }; + + const rejectingFn = async () => assert.fail(); + + let promise = assert.doesNotReject(rejectingFn, handler1); + promises.push(assert.rejects(promise, handler2)); + + promise = assert.doesNotReject(rejectingFn(), handler1); + promises.push(assert.rejects(promise, handler2)); + + promise = assert.doesNotReject(() => assert.fail(), common.mustNotCall()); + promises.push(assert.rejects(promise, handler1)); +} + +promises.push(assert.rejects( + assert.doesNotReject(123), { - const THROWN_ERROR = new Error(); - - await assert.rejects(() => { - throw THROWN_ERROR; - }).then(common.mustNotCall()) - .catch( - common.mustCall((err) => { - assert.strictEqual(err, THROWN_ERROR); - }) - ); + code: 'ERR_INVALID_ARG_TYPE', + message: 'The "block" argument must be one of type ' + + 'Function or Promise. Received type number' } -})().then(common.mustCall()); +)); + +// Make sure all async code gets properly executed. +Promise.all(promises).then(common.mustCall()); |