diff options
Diffstat (limited to 'doc/api/assert.md')
-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 |