| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -361,8 +361,13 @@ If the values are not strictly equal, an `AssertionError` is thrown with a | |||
| 361 | 361 | ||
| 362 | 362 | ## assert.throws(block[, error][, message]) | |
| 363 | 363 | ||
| 364 | - Expects the function `block` to throw an error. If specified, `error` can be a | ||
| 365 | - constructor, [`RegExp`][], or validation function. | ||
| 364 | + Expects the function `block` to throw an error. | ||
| 365 | + | ||
| 366 | + If specified, `error` can be a constructor, [`RegExp`][], or validation | ||
| 367 | + function. | ||
| 368 | + | ||
| 369 | + If specified, `message` will be the message provided by the `AssertionError` if | ||
| 370 | + the block fails to throw. | ||
| 366 | 371 | ||
| 367 | 372 | Validate instanceof using constructor: | |
| 368 | 373 | ||
@@ -402,6 +407,18 @@ assert.throws( | |||
| 402 | 407 | ); | |
| 403 | 408 | ``` | |
| 404 | 409 | ||
| 410 | + Note that `error` can not be a string. If a string is provided as the second | ||
| 411 | + argument, then `error` is assumed to be omitted and the string will be used for | ||
| 412 | + `message` instead. This can lead to easy-to-miss mistakes: | ||
| 413 | + | ||
| 414 | + ```js | ||
| 415 | + // THIS IS A MISTAKE! DO NOT DO THIS! | ||
| 416 | + assert.throws(myFunction, 'missing foo', 'did not throw with expected message'); | ||
| 417 | + | ||
| 418 | + // Do this instead. | ||
| 419 | + assert.throws(myFunction, /missing foo/, 'did not throw with expected message'); | ||
| 420 | + ``` | ||
| 421 | + | ||
| 405 | 422 | [Locked]: documentation.html#documentation_stability_index | |
| 406 | 423 | [`assert.deepEqual()`]: #assert_assert_deepequal_actual_expected_message | |
| 407 | 424 | [`assert.deepStrictEqual()`]: #assert_assert_deepstrictequal_actual_expected_message | |
| Back | FazBrowse Home | New Git URL |
0 commit comments