diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2017-12-10 03:54:44 +0300 |
---|---|---|
committer | Myles Borins <mylesborins@google.com> | 2018-10-31 20:45:06 +0300 |
commit | f2af930ebbd1ab4e4b8b929701d110c0c1983832 (patch) | |
tree | 929b8fb54059b0579b4b194cabb3d789fc38b170 /doc | |
parent | 147aeedc8d6d58313778d3bb030aad7858b84d48 (diff) |
assert: .throws accept objects
From now on it is possible to use a validation object in throws
instead of the other possibilites.
Backport-PR-URL: https://github.com/nodejs/node/pull/23223
PR-URL: https://github.com/nodejs/node/pull/17584
Refs: https://github.com/nodejs/node/pull/17557
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ron Korving <ron@ronkorving.nl>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Diffstat (limited to 'doc')
-rw-r--r-- | doc/api/assert.md | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/doc/api/assert.md b/doc/api/assert.md index b1c7168e94c..dbd32096b5c 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -635,18 +635,21 @@ If the values are not strictly equal, an `AssertionError` is thrown with a <!-- YAML added: v0.1.21 changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/REPLACEME + description: The `error` parameter can now be an object as well. - version: v4.2.0 pr-url: https://github.com/nodejs/node/pull/3276 description: The `error` parameter can now be an arrow function. --> * `block` {Function} -* `error` {RegExp|Function} +* `error` {RegExp|Function|object} * `message` {any} Expects the function `block` to throw an error. -If specified, `error` can be a constructor, [`RegExp`][], or validation -function. +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 throw. @@ -689,6 +692,23 @@ assert.throws( ); ``` +Custom error object / error instance: + +```js +assert.throws( + () => { + const err = new TypeError('Wrong value'); + err.code = 404; + throw err; + }, + { + name: 'TypeError', + message: 'Wrong value' + // Note that only properties on the error object will be tested! + } +); +``` + Note that `error` can not be a string. If a string is provided as the second argument, then `error` is assumed to be omitted and the string will be used for `message` instead. This can lead to easy-to-miss mistakes. Please read the |