diff options
author | Rich Trott <rtrott@gmail.com> | 2016-04-03 12:11:10 -0700 |
---|---|---|
committer | Myles Borins <mborins@us.ibm.com> | 2016-04-11 13:00:55 -0400 |
commit | 8c2befe176a7ea5317767d21d659553a94f11541 (patch) | |
tree | 04e3442f68837007b6e80b66e836a4156db9bfc5 | |
parent | ac40a4510d19b4982399c3e19d8a28a1a7c28a48 (diff) | |
download | nodejs-8c2befe176a7ea5317767d21d659553a94f11541.tar.gz nodejs-8c2befe176a7ea5317767d21d659553a94f11541.tar.bz2 nodejs-8c2befe176a7ea5317767d21d659553a94f11541.zip |
doc: note assert.throws() pitfall
PR-URL: https://github.com/nodejs/node/pull/6029
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
-rw-r--r-- | doc/api/assert.markdown | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/doc/api/assert.markdown b/doc/api/assert.markdown index 6bbbf6bd6..35dbc3d21 100644 --- a/doc/api/assert.markdown +++ b/doc/api/assert.markdown @@ -361,8 +361,13 @@ If the values are not strictly equal, an `AssertionError` is thrown with a ## assert.throws(block[, error][, message]) -Expects the function `block` to throw an error. If specified, `error` can be a -constructor, [`RegExp`][], or validation function. +Expects the function `block` to throw an error. + +If specified, `error` can be a constructor, [`RegExp`][], or validation +function. + +If specified, `message` will be the message provided by the `AssertionError` if +the block fails to throw. Validate instanceof using constructor: @@ -402,6 +407,18 @@ assert.throws( ); ``` +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: + +```js +// THIS IS A MISTAKE! DO NOT DO THIS! +assert.throws(myFunction, 'missing foo', 'did not throw with expected message'); + +// Do this instead. +assert.throws(myFunction, /missing foo/, 'did not throw with expected message'); +``` + [Locked]: documentation.html#documentation_stability_index [`assert.deepEqual()`]: #assert_assert_deepequal_actual_expected_message [`assert.deepStrictEqual()`]: #assert_assert_deepstrictequal_actual_expected_message |