diff options
author | feugy <damien.feugas@gmail.com> | 2018-01-12 02:16:41 +0300 |
---|---|---|
committer | Myles Borins <mylesborins@google.com> | 2018-11-04 15:30:51 +0300 |
commit | 3babc5bb533d94e6355d062233fbe05744603115 (patch) | |
tree | 37359302d360c32fc34a4b52d735b904f6dc903b /doc | |
parent | 7f34c277accdfffe0b87b24f5e2fda889b1ac924 (diff) |
assert: add rejects() and doesNotReject()
Implement asynchronous equivalent for assert.throws() and
assert.doesNotThrow().
Backport-PR-URL: https://github.com/nodejs/node/pull/24019
PR-URL: https://github.com/nodejs/node/pull/18023
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Diffstat (limited to 'doc')
-rw-r--r-- | doc/api/assert.md | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/doc/api/assert.md b/doc/api/assert.md index dbd32096b5c..6cc3cd561eb 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -242,6 +242,43 @@ If the values are not equal, an `AssertionError` is thrown with a `message` property set equal to the value of the `message` parameter. If the `message` parameter is undefined, a default error message is assigned. +## assert.doesNotReject(block[, error][, message]) +<!-- YAML +added: REPLACEME +--> +* `block` {Function} +* `error` {RegExp|Function} +* `message` {any} + +Awaits for the promise returned by function `block` to complete and not be +rejected. See [`assert.rejects()`][] for more details. + +When `assert.doesNotReject()` is called, it will immediately call the `block` +function, and awaits for completion. + +Besides the async nature to await the completion behaves identical to +[`assert.doesNotThrow()`][]. + +```js +(async () => { + await assert.doesNotReject( + async () => { + throw new TypeError('Wrong value'); + }, + SyntaxError + ); +})(); +``` + +```js +assert.doesNotReject( + () => Promise.reject(new TypeError('Wrong value')), + SyntaxError +).then(() => { + // ... +}); +``` + ## assert.doesNotThrow(block[, error][, message]) <!-- YAML added: v0.1.21 @@ -631,6 +668,48 @@ If the values are not strictly equal, an `AssertionError` is thrown with a `message` property set equal to the value of the `message` parameter. If the `message` parameter is undefined, a default error message is assigned. +## assert.rejects(block[, error][, message]) +<!-- YAML +added: REPLACEME +--> +* `block` {Function} +* `error` {RegExp|Function|Object} +* `message` {any} + +Awaits for promise returned by function `block` to be rejected. + +When `assert.rejects()` is called, it will immediately call the `block` +function, and awaits for completion. + +Besides the async nature to await the completion behaves identical to +[`assert.throws()`][]. + +If specified, `error` can be a constructor, [`RegExp`][], a validation +function, or an object where each property will be tested for. + +If specified, `message` will be the message provided by the `AssertionError` if +the block fails to reject. + +```js +(async () => { + await assert.rejects( + async () => { + throw new Error('Wrong value'); + }, + Error + ); +})(); +``` + +```js +assert.rejects( + () => Promise.reject(new Error('Wrong value')), + Error +).then(() => { + // ... +}); +``` + ## assert.throws(block[, error][, message]) <!-- YAML added: v0.1.21 @@ -786,6 +865,7 @@ For more information, see [`assert.ok()`]: #assert_assert_ok_value_message [`assert.strictEqual()`]: #assert_assert_strictequal_actual_expected_message [`assert.throws()`]: #assert_assert_throws_block_error_message +[`assert.rejects()`]: #assert_assert_rejects_block_error_message [`strict mode`]: #assert_strict_mode [Abstract Equality Comparison]: https://tc39.github.io/ecma262/#sec-abstract-equality-comparison [Object.prototype.toString()]: https://tc39.github.io/ecma262/#sec-object.prototype.tostring |