Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8c2befe

Browse filesBrowse files
TrottMyles Borins
authored andcommitted
doc: note assert.throws() pitfall
PR-URL: #6029 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent ac40a45 commit 8c2befe
Copy full SHA for 8c2befe

File tree

Expand file treeCollapse file tree

1 file changed

+19
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+19
-2
lines changed
Open diff view settings
Collapse file

‎doc/api/assert.markdown‎

Copy file name to clipboardExpand all lines: doc/api/assert.markdown
+19-2Lines changed: 19 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,13 @@ If the values are not strictly equal, an `AssertionError` is thrown with a
361361

362362
## assert.throws(block[, error][, message])
363363

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.
366371

367372
Validate instanceof using constructor:
368373

@@ -402,6 +407,18 @@ assert.throws(
402407
);
403408
```
404409

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+
405422
[Locked]: documentation.html#documentation_stability_index
406423
[`assert.deepEqual()`]: #assert_assert_deepequal_actual_expected_message
407424
[`assert.deepStrictEqual()`]: #assert_assert_deepstrictequal_actual_expected_message

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.