Skip to content

Commit 5bd9d68

Browse files
BridgeARjasnell
authored andcommitted
doc: improve assert documentation
This adds a example to `assert.throws` to document checking against error instances. It also makes it clearer what some arguments can receive as input. PR-URL: #19885 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent c667c87 commit 5bd9d68

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

doc/api/assert.md

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ is little benefit by catching a rejection and then rejecting it again. Instead,
395395
consider adding a comment next to the specific code path that should not reject
396396
and keep error messages as expressive as possible.
397397

398+
If specified, `error` can be a [`Class`][], [`RegExp`][] or a validation
399+
function. See [`assert.throws()`][] for more details.
400+
398401
Besides the async nature to await the completion behaves identically to
399402
[`assert.doesNotThrow()`][].
400403

@@ -431,8 +434,7 @@ changes:
431434
* `error` {RegExp|Function}
432435
* `message` {any}
433436

434-
Asserts that the function `block` does not throw an error. See
435-
[`assert.throws()`][] for more details.
437+
Asserts that the function `block` does not throw an error.
436438

437439
Please note: Using `assert.doesNotThrow()` is actually not useful because there
438440
is no benefit by catching an error and then rethrowing it. Instead, consider
@@ -447,6 +449,9 @@ parameter, then an `AssertionError` is thrown. If the error is of a different
447449
type, or if the `error` parameter is undefined, the error is propagated back
448450
to the caller.
449451

452+
If specified, `error` can be a [`Class`][], [`RegExp`][] or a validation
453+
function. See [`assert.throws()`][] for more details.
454+
450455
The following, for instance, will throw the [`TypeError`][] because there is no
451456
matching error type in the assertion:
452457

@@ -483,7 +488,7 @@ assert.doesNotThrow(
483488
() => {
484489
throw new TypeError('Wrong value');
485490
},
486-
TypeError,
491+
/Wrong value/,
487492
'Whoops'
488493
);
489494
// Throws: AssertionError: Got unwanted exception (TypeError). Whoops
@@ -916,7 +921,7 @@ assert(0);
916921
added: REPLACEME
917922
-->
918923
* `block` {Function|Promise}
919-
* `error` {RegExp|Function|Object}
924+
* `error` {RegExp|Function|Object|Error}
920925
* `message` {any}
921926

922927
Awaits the `block` promise or, if `block` is a function, immediately calls the
@@ -930,8 +935,10 @@ checking the error handler.
930935
Besides the async nature to await the completion behaves identically to
931936
[`assert.throws()`][].
932937

933-
If specified, `error` can be a constructor, [`RegExp`][], a validation
934-
function, or an object where each property will be tested for.
938+
If specified, `error` can be a [`Class`][], [`RegExp`][], a validation function,
939+
an object where each property will be tested for, or an instance of error where
940+
each property will be tested for including the non-enumerable `message` and
941+
`name` properties.
935942

936943
If specified, `message` will be the message provided by the `AssertionError` if
937944
the block fails to reject.
@@ -1011,13 +1018,15 @@ changes:
10111018
description: The `error` parameter can now be an arrow function.
10121019
-->
10131020
* `block` {Function}
1014-
* `error` {RegExp|Function|Object}
1021+
* `error` {RegExp|Function|Object|Error}
10151022
* `message` {any}
10161023

10171024
Expects the function `block` to throw an error.
10181025

1019-
If specified, `error` can be a constructor, [`RegExp`][], a validation
1020-
function, or an object where each property will be tested for.
1026+
If specified, `error` can be a [`Class`][], [`RegExp`][], a validation function,
1027+
an object where each property will be tested for, or an instance of error where
1028+
each property will be tested for including the non-enumerable `message` and
1029+
`name` properties.
10211030

10221031
If specified, `message` will be the message provided by the `AssertionError` if
10231032
the block fails to throw.
@@ -1066,10 +1075,11 @@ assert.throws(
10661075
Custom error object / error instance:
10671076

10681077
```js
1078+
const err = new TypeError('Wrong value');
1079+
err.code = 404;
1080+
10691081
assert.throws(
10701082
() => {
1071-
const err = new TypeError('Wrong value');
1072-
err.code = 404;
10731083
throw err;
10741084
},
10751085
{
@@ -1078,6 +1088,16 @@ assert.throws(
10781088
// Note that only properties on the error object will be tested!
10791089
}
10801090
);
1091+
1092+
// Fails due to the different `message` and `name` properties:
1093+
assert.throws(
1094+
() => {
1095+
const otherErr = new Error('Not found');
1096+
otherErr.code = 404;
1097+
throw otherErr;
1098+
},
1099+
err // This tests for `message`, `name` and `code`.
1100+
);
10811101
```
10821102

10831103
Note that `error` cannot be a string. If a string is provided as the second
@@ -1118,6 +1138,7 @@ assert.throws(throwingFirst, /Second$/);
11181138
Due to the confusing notation, it is recommended not to use a string as the
11191139
second argument. This might lead to difficult-to-spot errors.
11201140

1141+
[`Class`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
11211142
[`Error.captureStackTrace`]: errors.html#errors_error_capturestacktrace_targetobject_constructoropt
11221143
[`Error`]: errors.html#errors_class_error
11231144
[`Map`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

0 commit comments

Comments
 (0)