diff options
author | feugy <damien.feugas@gmail.com> | 2018-01-12 00:16:41 +0100 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-03-11 04:58:32 +0100 |
commit | 599337f43e0e0d66263e69d70edab26b61d3038a (patch) | |
tree | 72211965a669694de7e4b148f14e4abf6460d510 /doc | |
parent | 580ad0157a4dcb21a8ed6b288defdd959711f364 (diff) | |
download | android-node-v8-599337f43e0e0d66263e69d70edab26b61d3038a.tar.gz android-node-v8-599337f43e0e0d66263e69d70edab26b61d3038a.tar.bz2 android-node-v8-599337f43e0e0d66263e69d70edab26b61d3038a.zip |
assert: add rejects() and doesNotReject()
Implement asynchronous equivalent for assert.throws() and
assert.doesNotThrow().
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 6728a819d3..591d26a515 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -312,6 +312,43 @@ parameter is undefined, a default error message is assigned. If the `message` parameter is an instance of an [`Error`][] then it will be thrown instead of the `AssertionError`. +## 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 @@ -841,6 +878,48 @@ If the values are not strictly equal, an `AssertionError` is thrown with a `message` parameter is an instance of an [`Error`][] then it will be thrown instead of the `AssertionError`. +## 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 @@ -977,6 +1056,7 @@ second argument. This might lead to difficult-to-spot errors. [`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 |