Skip to content

Commit 9921539

Browse files
committed
doc: improve assert function descriptions
This commit uses more consistent language in function descriptions and adds function parameter and return type information.
1 parent 51c5322 commit 9921539

File tree

1 file changed

+134
-72
lines changed

1 file changed

+134
-72
lines changed

doc/api/assert.md

Lines changed: 134 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ used in application code via `require('assert')`. However, `assert` is not a
88
testing framework, and is not intended to be used as a general purpose assertion
99
library.
1010

11-
The API for the `assert` module is [Locked][]. This means that there will be no
12-
additions or changes to any of the methods implemented and exposed by
13-
the module.
11+
The API for the `assert` module is [Locked]. This means that there will be no
12+
additions or changes to any of the methods implemented and exposed by this module.
1413

1514
## assert(value[, message])
1615
<!-- YAML
@@ -24,14 +23,21 @@ An alias of [`assert.ok()`].
2423
added: v0.1.21
2524
-->
2625

27-
Tests for deep equality between the `actual` and `expected` parameters.
28-
Primitive values are compared with the equal comparison operator ( `==` ).
26+
* `actual` {mixed} A value to check
27+
* `expected` {mixed} The expected value of `actual`
28+
* `message` {String} The exception message to use when `actual` and `expected`
29+
are not equal. If not supplied, then a generated message containing `actual`
30+
and `expected` is used.
31+
* Return: `undefined`
2932

30-
Only enumerable "own" properties are considered. The `deepEqual()`
33+
Tests *deep* equality between `actual` and `expected` as determined by the `==`
34+
operator. If `actual` and `expected` are not equal, then an `AssertionError`
35+
exception is thrown.
36+
37+
Only enumerable "own" properties are considered. The `assert.deepEqual()`
3138
implementation does not test object prototypes, attached symbols, or
3239
non-enumerable properties. This can lead to some potentially surprising
33-
results. For example, the following example does not throw an `AssertionError`
34-
because the properties on the [`Error`][] object are non-enumerable:
40+
results.
3541

3642
Example: The following does not throw an `AssertionError` because the properties
3743
on the [`Error`] object are non-enumerable
@@ -44,7 +50,7 @@ assert.deepEqual(Error('a'), Error('b'));
4450
```
4551

4652
"Deep" equality means that the enumerable "own" properties of child objects
47-
are evaluated also:
53+
are evaluated as well.
4854

4955
Examples:
5056

@@ -83,18 +89,22 @@ assert.deepEqual(obj1, obj2);
8389
assert.deepEqual(obj1, obj4);
8490
```
8591

86-
If the values are not equal, an `AssertionError` is thrown with a `message`
87-
property set equal to the value of the `message` parameter. If the `message`
88-
parameter is undefined, a default error message is assigned.
89-
9092
## assert.deepStrictEqual(actual, expected[, message])
9193
<!-- YAML
9294
added: v1.2.0
9395
-->
9496

95-
Generally identical to `assert.deepEqual()` with two exceptions. First,
96-
primitive values are compared using the strict equality operator ( `===` ).
97-
Second, object comparisons include a strict equality check of their prototypes.
97+
* `actual` {mixed} A value to check
98+
* `expected` {mixed} The expected value of `actual`
99+
* `message` {String} The exception message to use when `actual` and `expected`
100+
are not equal. If not supplied, then a generated message containing `actual`
101+
and `expected` is used.
102+
103+
Tests *deep* equality between `actual` and `expected` as determined by the `===`
104+
operator. If `actual` and `expected` are not equal, then an `AssertionError`
105+
exception is thrown.
106+
107+
Object comparisons include a strict equal check of their prototypes.
98108

99109
Example:
100110

@@ -112,25 +122,24 @@ const obj2 = {
112122
assert.deepStrictEqual(obj1, obj2);
113123
```
114124

115-
If the values are not equal, an `AssertionError` is thrown with a `message`
116-
property set equal to the value of the `message` parameter. If the `message`
117-
parameter is undefined, a default error message is assigned.
118-
119125
## assert.doesNotThrow(block[, error][, message])
120126
<!-- YAML
121127
added: v0.1.21
122128
-->
123129

124-
Asserts that the function `block` does not throw an error. See
125-
[`assert.throws()`][] for more details.
130+
* `block` {Function} A function that is not expected to throw an exception
131+
* `error` {Function | RegExp} A constructor, regular expression, or validation
132+
function to be used when matching an exception thrown by `block`
133+
* `message` {String} A message that is appended to the exception thrown when
134+
`block` fails to throw an exception.
135+
* Return: `undefined`
126136

127-
When `assert.doesNotThrow()` is called, it will immediately call the `block`
128-
function.
137+
Calls the function `block`, not expecting an exception to be thrown. If `block`
138+
throws an exception that matches `error`, an `AssertionError` exception is thrown.
139+
If the exception does *not* match `error` (or `error` is not supplied), then the
140+
original exception is re-thrown.
129141

130-
If an error is thrown and it is the same type as that specified by the `error`
131-
parameter, then an `AssertionError` is thrown. If the error is of a different
132-
type, or if the `error` parameter is undefined, the error is propagated back
133-
to the caller.
142+
This function is the opposite of [`assert.throws()`].
134143

135144
Examples:
136145

@@ -168,8 +177,16 @@ assert.doesNotThrow(
168177
added: v0.1.21
169178
-->
170179

171-
Tests shallow, coercive equality between the `actual` and `expected` parameters
172-
using the equal comparison operator ( `==` ).
180+
* `actual` {mixed} A value to check
181+
* `expected` {mixed} The expected value of `actual`
182+
* `message` {String} The exception message to use when `actual` and `expected`
183+
are not equal. If not supplied, then a generated message containing `actual`
184+
and `expected` is used.
185+
* Return: `undefined`
186+
187+
Tests equality between `actual` and `expected` as determined by the (shallow
188+
and coercive) `==` operator. If `actual` and `expected` are not equal, then an
189+
`AssertionError` exception is thrown.
173190

174191
Examples:
175192

@@ -189,18 +206,21 @@ assert.equal(1, 2);
189206
assert.equal({a: {b: 1}}, {a: {b: 1}});
190207
```
191208

192-
If the values are not equal, an `AssertionError` is thrown with a `message`
193-
property set equal to the value of the `message` parameter. If the `message`
194-
parameter is undefined, a default error message is assigned.
195-
196209
## assert.fail(actual, expected, message, operator)
197210
<!-- YAML
198211
added: v0.1.21
199212
-->
200213

201-
Throws an `AssertionError`. If `message` is falsy, the error message is set as
202-
the values of `actual` and `expected` separated by the provided `operator`.
203-
Otherwise, the error message is the value of `message`.
214+
* `actual` {mixed} A value to check
215+
* `expected` {mixed} The expected value of `actual`
216+
* `message` {String} The exception message to use. If not supplied, then a
217+
generated message containing `actual` and `expected` is used.
218+
* `operator` {String} If `message` is not specified, this is a comparison operator
219+
that is inserted between `actual` and `expected` in the generated exception
220+
message.
221+
* Return: `undefined`
222+
223+
Always throws an `AssertionError`.
204224

205225
Examples:
206226

@@ -219,8 +239,11 @@ assert.fail(1, 2, 'whoops', '>');
219239
added: v0.1.97
220240
-->
221241

222-
Throws `value` if `value` is truthy. This is useful when testing the `error`
223-
argument in callbacks.
242+
* `value` {mixed}
243+
* Return: `undefined`
244+
245+
Throws `value` if `value` evaluates to `true`. This is useful when testing the
246+
error argument in callbacks.
224247

225248
Examples:
226249

@@ -245,7 +268,22 @@ assert.ifError(new Error());
245268
added: v0.1.21
246269
-->
247270

248-
Tests for any deep inequality. Opposite of [`assert.deepEqual()`][].
271+
* `actual` {mixed} A value to check
272+
* `expected` {mixed} The expected value of `actual`
273+
* `message` {String} The exception message to use when `actual` and `expected`
274+
are equal. If not supplied, then a generated message containing `actual` and
275+
`expected` is used.
276+
277+
Tests *deep* inequality between `actual` and `expected` as determined by the `!=`
278+
operator. If `actual` and `expected` are equal, then an `AssertionError`
279+
exception is thrown.
280+
281+
Only enumerable "own" properties are considered. The `assert.notDeepEqual()`
282+
implementation does not test object prototypes, attached symbols, or
283+
non-enumerable properties. This can lead to some potentially surprising
284+
results.
285+
286+
This function is the opposite of [`assert.deepEqual()`].
249287

250288
Examples:
251289

@@ -282,16 +320,24 @@ assert.notDeepEqual(obj1, obj1);
282320
assert.notDeepEqual(obj1, obj3);
283321
```
284322

285-
If the values are deeply equal, an `AssertionError` is thrown with a `message`
286-
property set equal to the value of the `message` parameter. If the `message`
287-
parameter is undefined, a default error message is assigned.
288-
289323
## assert.notDeepStrictEqual(actual, expected[, message])
290324
<!-- YAML
291325
added: v1.2.0
292326
-->
293327

294-
Tests for deep strict inequality. Opposite of [`assert.deepStrictEqual()`][].
328+
* `actual` {mixed} A value to check
329+
* `expected` {mixed} The expected value of `actual`
330+
* `message` {String} The exception message to use when `actual` and `expected`
331+
are equal. If not supplied, then a generated message containing `actual` and
332+
`expected` is used.
333+
334+
Tests *deep* inequality between `actual` and `expected` as determined by the `!==`
335+
operator. If `actual` and `expected` are equal, then an `AssertionError`
336+
exception is thrown.
337+
338+
Object comparisons include a not strict equal check of their prototypes.
339+
340+
This function is the opposite of [`assert.deepStrictEqual()`].
295341

296342
Examples:
297343

@@ -309,17 +355,20 @@ const obj2 = {
309355
assert.notDeepStrictEqual(obj1, obj2);
310356
```
311357

312-
If the values are deeply and strictly equal, an `AssertionError` is thrown
313-
with a `message` property set equal to the value of the `message` parameter. If
314-
the `message` parameter is undefined, a default error message is assigned.
315-
316358
## assert.notEqual(actual, expected[, message])
317359
<!-- YAML
318360
added: v0.1.21
319361
-->
320362

321-
Tests shallow, coercive inequality with the not equal comparison operator
322-
( `!=` ).
363+
* `actual` {mixed} A value to check
364+
* `expected` {mixed} The expected value of `actual`
365+
* `message` {String} The exception message to use when `actual` and `expected`
366+
are equal. If not supplied, then a generated message containing `actual` and
367+
`expected` is used.
368+
369+
Tests inequality between `actual` and `expected` as determined by the (shallow
370+
and coercive) `!=` operator. If `actual` and `expected` are equal, then an
371+
`AssertionError` exception is thrown.
323372

324373
Examples:
325374

@@ -336,17 +385,20 @@ assert.notEqual(1, 1);
336385
assert.notEqual(1, '1');
337386
```
338387

339-
If the values are equal, an `AssertionError` is thrown with a `message`
340-
property set equal to the value of the `message` parameter. If the `message`
341-
parameter is undefined, a default error message is assigned.
342-
343388
## assert.notStrictEqual(actual, expected[, message])
344389
<!-- YAML
345390
added: v0.1.21
346391
-->
347392

348-
Tests strict inequality as determined by the strict not equal operator
349-
( `!==` ).
393+
* `actual` {mixed} A value to check
394+
* `expected` {mixed} The expected value of `actual`
395+
* `message` {String} The exception message to use when `actual` and `expected`
396+
are equal. If not supplied, then a generated message containing `actual` and
397+
`expected` is used.
398+
399+
Tests inequality between `actual` and `expected` as determined by the `!==`
400+
operator. If `actual` and `expected` are equal, then an `AssertionError` exception
401+
is thrown.
350402

351403
Examples:
352404

@@ -368,12 +420,15 @@ assert.notStrictEqual(1, 1);
368420
added: v0.1.21
369421
-->
370422

371-
Tests if `value` is truthy. It is equivalent to
372-
`assert.equal(!!value, true, message)`.
423+
* `value` {mixed} A value to check
424+
* `message` {String} The exception message to use when `value` does not evaluate
425+
to `true`. If not supplied, then a generated message containing `value` is used.
426+
* Return: `undefined`
373427

374-
If `value` is not truthy, an `AssertionError` is thrown with a `message`
375-
property set equal to the value of the `message` parameter. If the `message`
376-
parameter is `undefined`, a default error message is assigned.
428+
Tests if `value` evaluates to `true`. If `value` does not evaluate to `true`,
429+
then an `AssertionError` exception is thrown.
430+
431+
This function is equivalent to `assert.equal(!!value, true, message)`.
377432

378433
Examples:
379434

@@ -401,7 +456,15 @@ assert.ok(false, 'it is false');
401456
added: v0.1.21
402457
-->
403458

404-
Tests strict equality as determined by the strict equality operator ( `===` ).
459+
* `actual` {mixed} A value to check
460+
* `expected` {mixed} The expected value of `actual`
461+
* `message` {String} The exception message to use when `actual` and `expected`
462+
are not equal. If not supplied, then a generated message containing `actual`
463+
and `expected` is used.
464+
465+
Tests equality between `actual` and `expected` as determined by the `===`
466+
operator. If `actual` and `expected` are not equal, then an `AssertionError`
467+
exception is thrown.
405468

406469
Examples:
407470

@@ -418,22 +481,21 @@ assert.strictEqual(1, 2);
418481
assert.strictEqual(1, '1');
419482
```
420483

421-
If the values are not strictly equal, an `AssertionError` is thrown with a
422-
`message` property set equal to the value of the `message` parameter. If the
423-
`message` parameter is undefined, a default error message is assigned.
424-
425484
## assert.throws(block[, error][, message])
426485
<!-- YAML
427486
added: v0.1.21
428487
-->
429488

430-
Expects the function `block` to throw an error.
489+
* `block` {Function} A function that is expected to throw an exception
490+
* `error` {Function | RegExp} A constructor, regular expression, or validation
491+
function to be used when matching an exception thrown by `block`
492+
* `message` {String} The exception message to use when `block` fails to throw an
493+
exception. If not supplied, then a generic, generated message is used.
494+
* Return: `undefined`
431495

432-
If specified, `error` can be a constructor, [`RegExp`][], or validation
433-
function.
496+
Calls the function `block`, expecting an exception to be thrown. If `block`
497+
does not throw an exception, an `AssertionError` exception is thrown.
434498

435-
If specified, `message` will be the message provided by the `AssertionError` if
436-
the block fails to throw.
437499
Example: Match a thrown exception by constructor
438500

439501
```js

0 commit comments

Comments
 (0)