Skip to content

Commit c1483ba

Browse files
BridgeARGeorge Adams
authored and
George Adams
committed
doc: improve assert documentation
This fixes the officially accepted message types for `assert.throws()`, `assert.rejects()`, `assert.doesNotThrow()` and `assert.doesNotReject()`. It also renames the `block` argument in those functions to `fn` and `promiseFn` for further clarity. PR-URL: #22692 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: George Adams <george.adams@uk.ibm.com>
1 parent dbf7203 commit c1483ba

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

doc/api/assert.md

+29-28
Original file line numberDiff line numberDiff line change
@@ -408,19 +408,19 @@ parameter is undefined, a default error message is assigned. If the `message`
408408
parameter is an instance of an [`Error`][] then it will be thrown instead of the
409409
`AssertionError`.
410410

411-
## assert.doesNotReject(block[, error][, message])
411+
## assert.doesNotReject(asyncFn[, error][, message])
412412
<!-- YAML
413413
added: v10.0.0
414414
-->
415-
* `block` {Function|Promise}
415+
* `asyncFn` {Function|Promise}
416416
* `error` {RegExp|Function}
417-
* `message` {string|Error}
417+
* `message` {string}
418418

419-
Awaits the `block` promise or, if `block` is a function, immediately calls the
420-
function and awaits the returned promise to complete. It will then check that
421-
the promise is not rejected.
419+
Awaits the `asyncFn` promise or, if `asyncFn` is a function, immediately
420+
calls the function and awaits the returned promise to complete. It will then
421+
check that the promise is not rejected.
422422

423-
If `block` is a function and it throws an error synchronously,
423+
If `asyncFn` is a function and it throws an error synchronously,
424424
`assert.doesNotReject()` will return a rejected `Promise` with that error. If
425425
the function does not return a promise, `assert.doesNotReject()` will return a
426426
rejected `Promise` with an [`ERR_INVALID_RETURN_VALUE`][] error. In both cases
@@ -455,7 +455,7 @@ assert.doesNotReject(Promise.reject(new TypeError('Wrong value')))
455455
});
456456
```
457457

458-
## assert.doesNotThrow(block[, error][, message])
458+
## assert.doesNotThrow(fn[, error][, message])
459459
<!-- YAML
460460
added: v0.1.21
461461
changes:
@@ -466,18 +466,18 @@ changes:
466466
pr-url: https://github.com/nodejs/node/pull/3276
467467
description: The `error` parameter can now be an arrow function.
468468
-->
469-
* `block` {Function}
469+
* `fn` {Function}
470470
* `error` {RegExp|Function}
471-
* `message` {string|Error}
471+
* `message` {string}
472472

473-
Asserts that the function `block` does not throw an error.
473+
Asserts that the function `fn` does not throw an error.
474474

475475
Please note: Using `assert.doesNotThrow()` is actually not useful because there
476476
is no benefit by catching an error and then rethrowing it. Instead, consider
477477
adding a comment next to the specific code path that should not throw and keep
478478
error messages as expressive as possible.
479479

480-
When `assert.doesNotThrow()` is called, it will immediately call the `block`
480+
When `assert.doesNotThrow()` is called, it will immediately call the `fn`
481481
function.
482482

483483
If an error is thrown and it is the same type as that specified by the `error`
@@ -964,19 +964,19 @@ assert(0);
964964
// assert(0)
965965
```
966966

967-
## assert.rejects(block[, error][, message])
967+
## assert.rejects(asyncFn[, error][, message])
968968
<!-- YAML
969969
added: v10.0.0
970970
-->
971-
* `block` {Function|Promise}
971+
* `asyncFn` {Function|Promise}
972972
* `error` {RegExp|Function|Object|Error}
973-
* `message` {string|Error}
973+
* `message` {string}
974974

975-
Awaits the `block` promise or, if `block` is a function, immediately calls the
976-
function and awaits the returned promise to complete. It will then check that
977-
the promise is rejected.
975+
Awaits the `asyncFn` promise or, if `asyncFn` is a function, immediately
976+
calls the function and awaits the returned promise to complete. It will then
977+
check that the promise is rejected.
978978

979-
If `block` is a function and it throws an error synchronously,
979+
If `asyncFn` is a function and it throws an error synchronously,
980980
`assert.rejects()` will return a rejected `Promise` with that error. If the
981981
function does not return a promise, `assert.rejects()` will return a rejected
982982
`Promise` with an [`ERR_INVALID_RETURN_VALUE`][] error. In both cases the error
@@ -991,7 +991,7 @@ each property will be tested for including the non-enumerable `message` and
991991
`name` properties.
992992

993993
If specified, `message` will be the message provided by the `AssertionError` if
994-
the block fails to reject.
994+
the `asyncFn` fails to reject.
995995

996996
```js
997997
(async () => {
@@ -1063,7 +1063,7 @@ If the values are not strictly equal, an `AssertionError` is thrown with a
10631063
`message` parameter is an instance of an [`Error`][] then it will be thrown
10641064
instead of the `AssertionError`.
10651065

1066-
## assert.throws(block[, error][, message])
1066+
## assert.throws(fn[, error][, message])
10671067
<!-- YAML
10681068
added: v0.1.21
10691069
changes:
@@ -1078,11 +1078,11 @@ changes:
10781078
pr-url: https://github.com/nodejs/node/pull/3276
10791079
description: The `error` parameter can now be an arrow function.
10801080
-->
1081-
* `block` {Function}
1081+
* `fn` {Function}
10821082
* `error` {RegExp|Function|Object|Error}
1083-
* `message` {string|Error}
1083+
* `message` {string}
10841084

1085-
Expects the function `block` to throw an error.
1085+
Expects the function `fn` to throw an error.
10861086

10871087
If specified, `error` can be a [`Class`][], [`RegExp`][], a validation function,
10881088
a validation object where each property will be tested for strict deep equality,
@@ -1091,8 +1091,9 @@ equality including the non-enumerable `message` and `name` properties. When
10911091
using an object, it is also possible to use a regular expression, when
10921092
validating against a string property. See below for examples.
10931093

1094-
If specified, `message` will be the message provided by the `AssertionError` if
1095-
the block fails to throw.
1094+
If specified, `message` will be appended to the message provided by the
1095+
`AssertionError` if the `fn` call fails to throw or in case the error validation
1096+
fails.
10961097

10971098
Custom validation object/error instance:
10981099

@@ -1258,12 +1259,12 @@ second argument. This might lead to difficult-to-spot errors.
12581259
[`WeakSet`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet
12591260
[`assert.deepEqual()`]: #assert_assert_deepequal_actual_expected_message
12601261
[`assert.deepStrictEqual()`]: #assert_assert_deepstrictequal_actual_expected_message
1261-
[`assert.doesNotThrow()`]: #assert_assert_doesnotthrow_block_error_message
1262+
[`assert.doesNotThrow()`]: #assert_assert_doesnotthrow_fn_error_message
12621263
[`assert.notDeepStrictEqual()`]: #assert_assert_notdeepstrictequal_actual_expected_message
12631264
[`assert.notStrictEqual()`]: #assert_assert_notstrictequal_actual_expected_message
12641265
[`assert.ok()`]: #assert_assert_ok_value_message
12651266
[`assert.strictEqual()`]: #assert_assert_strictequal_actual_expected_message
1266-
[`assert.throws()`]: #assert_assert_throws_block_error_message
1267+
[`assert.throws()`]: #assert_assert_throws_fn_error_message
12671268
[`strict mode`]: #assert_strict_mode
12681269
[Abstract Equality Comparison]: https://tc39.github.io/ecma262/#sec-abstract-equality-comparison
12691270
[Object.prototype.toString()]: https://tc39.github.io/ecma262/#sec-object.prototype.tostring

0 commit comments

Comments
 (0)