Skip to content

Commit dd25984

Browse files
TrottMyles Borins
authored and
Myles Borins
committed
doc: note assert.throws() pitfall
PR-URL: #6029 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 0127c2b commit dd25984

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

doc/api/assert.markdown

+19-2
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)