-
Notifications
You must be signed in to change notification settings - Fork 29.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: #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>
- Loading branch information
Showing
3 changed files
with
161 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); |