Skip to content

Commit 37828f9

Browse files
committed
doc: improve assert documentation
1) Separate all loose and strict functions. 2) Stronger outline the used comparison rules in (not)deepStrictEqual 3) Fix SameValue comparison info
1 parent e46c3f7 commit 37828f9

File tree

1 file changed

+44
-46
lines changed

1 file changed

+44
-46
lines changed

doc/api/assert.md

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ assert.deepEqual(obj1, obj4);
104104
If the values are not equal, an `AssertionError` is thrown with a `message`
105105
property set equal to the value of the `message` parameter. If the `message`
106106
parameter is undefined, a default error message is assigned. If the `message`
107-
parameter is an instance of an `Error` then it will be thrown instead of the
107+
parameter is an instance of an [`Error`][] then it will be thrown instead of the
108108
`AssertionError`.
109109

110110
## assert.deepStrictEqual(actual, expected[, message])
@@ -137,50 +137,50 @@ changes:
137137
* `expected` {any}
138138
* `message` {any}
139139

140-
Identical to [`assert.deepEqual()`][] with the following exceptions:
140+
Tests for deep equality between the `actual` and `expected` parameters.
141+
"Deep" equality means that the enumerable "own" properties of child objects
142+
are recursively evaluated also by the following rules.
141143

142-
1. Primitive values besides `NaN` are compared using the [Strict Equality
143-
Comparison][] ( `===` ). Set and Map values, Map keys and `NaN` are compared
144-
using the [SameValueZero][] comparison (which means they are free of the
145-
[caveats][]).
146-
2. [`[[Prototype]]`][prototype-spec] of objects are compared using
144+
### Comparison details
145+
146+
* Primitive values are compared using the [SameValue Comparison][], used by
147+
[`Object.is()`][].
148+
* [Type tags][Object.prototype.toString()] of objects should be the same.
149+
* [`[[Prototype]]`][prototype-spec] of objects are compared using
147150
the [Strict Equality Comparison][] too.
148-
3. [Type tags][Object.prototype.toString()] of objects should be the same.
149-
4. [Object wrappers][] are compared both as objects and unwrapped values.
150-
5. `0` and `-0` are not considered equal.
151-
6. Enumerable own [`Symbol`][] properties are compared as well.
151+
* Only [enumerable "own" properties][] are considered.
152+
* [`Error`][] names and messages are always compared, even if these are not
153+
enumerable properties.
154+
* Enumerable own [`Symbol`][] properties are compared as well.
155+
* [Object wrappers][] are compared both as objects and unwrapped values.
156+
* Object properties are compared unordered.
157+
* Map keys and Set items are compared unordered.
158+
* Recursion stops when both sides differ or both sides encounter a circular
159+
reference.
152160

153161
```js
154162
const assert = require('assert');
155163

156-
assert.deepEqual({ a: 1 }, { a: '1' });
157-
// OK, because 1 == '1'
158-
159164
assert.deepStrictEqual({ a: 1 }, { a: '1' });
160165
// AssertionError: { a: 1 } deepStrictEqual { a: '1' }
161-
// because 1 !== '1' using strict equality
166+
// because 1 !== '1' using SameValue comparison
162167

163168
// The following objects don't have own properties
164169
const date = new Date();
165170
const object = {};
166171
const fakeDate = {};
167-
168172
Object.setPrototypeOf(fakeDate, Date.prototype);
169173

170-
assert.deepEqual(object, fakeDate);
171-
// OK, doesn't check [[Prototype]]
172174
assert.deepStrictEqual(object, fakeDate);
173175
// AssertionError: {} deepStrictEqual Date {}
174176
// Different [[Prototype]]
175177

176-
assert.deepEqual(date, fakeDate);
177-
// OK, doesn't check type tags
178178
assert.deepStrictEqual(date, fakeDate);
179179
// AssertionError: 2017-03-11T14:25:31.849Z deepStrictEqual Date {}
180180
// Different type tags
181181

182182
assert.deepStrictEqual(NaN, NaN);
183-
// OK, because of the SameValueZero comparison
183+
// OK, because of the SameValue comparison
184184

185185
assert.deepStrictEqual(new Number(1), new Number(2));
186186
// Fails because the wrapped number is unwrapped and compared as well.
@@ -203,7 +203,7 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
203203
If the values are not equal, an `AssertionError` is thrown with a `message`
204204
property set equal to the value of the `message` parameter. If the `message`
205205
parameter is undefined, a default error message is assigned. If the `message`
206-
parameter is an instance of an `Error` then it will be thrown instead of the
206+
parameter is an instance of an [`Error`][] then it will be thrown instead of the
207207
`AssertionError`.
208208

209209
## assert.doesNotThrow(block[, error][, message])
@@ -299,7 +299,7 @@ assert.equal({ a: { b: 1 } }, { a: { b: 1 } });
299299
If the values are not equal, an `AssertionError` is thrown with a `message`
300300
property set equal to the value of the `message` parameter. If the `message`
301301
parameter is undefined, a default error message is assigned. If the `message`
302-
parameter is an instance of an `Error` then it will be thrown instead of the
302+
parameter is an instance of an [`Error`][] then it will be thrown instead of the
303303
`AssertionError`.
304304

305305
## assert.fail([message])
@@ -315,7 +315,7 @@ added: v0.1.21
315315

316316
Throws an `AssertionError`. If `message` is falsy, the error message is set as
317317
the values of `actual` and `expected` separated by the provided `operator`. If
318-
the `message` parameter is an instance of an `Error` then it will be thrown
318+
the `message` parameter is an instance of an [`Error`][] then it will be thrown
319319
instead of the `AssertionError`. If just the two `actual` and `expected`
320320
arguments are provided, `operator` will default to `'!='`. If `message` is
321321
provided only it will be used as the error message, the other arguments will be
@@ -450,7 +450,7 @@ assert.notDeepEqual(obj1, obj4);
450450
If the values are deeply equal, an `AssertionError` is thrown with a `message`
451451
property set equal to the value of the `message` parameter. If the `message`
452452
parameter is undefined, a default error message is assigned. If the `message`
453-
parameter is an instance of an `Error` then it will be thrown instead of the
453+
parameter is an instance of an [`Error`][] then it will be thrown instead of the
454454
`AssertionError`.
455455

456456
## assert.notDeepStrictEqual(actual, expected[, message])
@@ -488,18 +488,15 @@ Tests for deep strict inequality. Opposite of [`assert.deepStrictEqual()`][].
488488
```js
489489
const assert = require('assert');
490490

491-
assert.notDeepEqual({ a: 1 }, { a: '1' });
492-
// AssertionError: { a: 1 } notDeepEqual { a: '1' }
493-
494491
assert.notDeepStrictEqual({ a: 1 }, { a: '1' });
495492
// OK
496493
```
497494

498495
If the values are deeply and strictly equal, an `AssertionError` is thrown with
499496
a `message` property set equal to the value of the `message` parameter. If the
500497
`message` parameter is undefined, a default error message is assigned. If the
501-
`message` parameter is an instance of an `Error` then it will be thrown instead
502-
of the `AssertionError`.
498+
`message` parameter is an instance of an [`Error`][] then it will be thrown
499+
instead of the `AssertionError`.
503500

504501
## assert.notEqual(actual, expected[, message])
505502
<!-- YAML
@@ -525,10 +522,10 @@ assert.notEqual(1, '1');
525522
// AssertionError: 1 != '1'
526523
```
527524

528-
If the values are equal, an `AssertionError` is thrown with a `message`
529-
property set equal to the value of the `message` parameter. If the `message`
530-
parameter is undefined, a default error message is assigned. If the `message`
531-
parameter is an instance of an `Error` then it will be thrown instead of the
525+
If the values are equal, an `AssertionError` is thrown with a `message` property
526+
set equal to the value of the `message` parameter. If the `message` parameter is
527+
undefined, a default error message is assigned. If the `message` parameter is an
528+
instance of an [`Error`][] then it will be thrown instead of the
532529
`AssertionError`.
533530

534531
## assert.notStrictEqual(actual, expected[, message])
@@ -543,7 +540,8 @@ changes:
543540
* `expected` {any}
544541
* `message` {any}
545542

546-
Tests equality determined by the [`Object.is()`][] comparison.
543+
Tests strict inequality between the `actual` and `expected` parameters as
544+
determined by the [SameValue Comparison][].
547545

548546
```js
549547
const assert = require('assert');
@@ -558,11 +556,11 @@ assert.notStrictEqual(1, '1');
558556
// OK
559557
```
560558

561-
If the values are strictly equal, an `AssertionError` is thrown with a
562-
`message` property set equal to the value of the `message` parameter. If the
563-
`message` parameter is undefined, a default error message is assigned. If the
564-
`message` parameter is an instance of an `Error` then it will be thrown instead
565-
of the `AssertionError`.
559+
If the values are strictly equal, an `AssertionError` is thrown with a `message`
560+
property set equal to the value of the `message` parameter. If the `message`
561+
parameter is undefined, a default error message is assigned. If the `message`
562+
parameter is an instance of an [`Error`][] then it will be thrown instead of the
563+
`AssertionError`.
566564

567565
## assert.ok(value[, message])
568566
<!-- YAML
@@ -577,7 +575,7 @@ Tests if `value` is truthy. It is equivalent to
577575
If `value` is not truthy, an `AssertionError` is thrown with a `message`
578576
property set equal to the value of the `message` parameter. If the `message`
579577
parameter is `undefined`, a default error message is assigned. If the `message`
580-
parameter is an instance of an `Error` then it will be thrown instead of the
578+
parameter is an instance of an [`Error`][] then it will be thrown instead of the
581579
`AssertionError`.
582580

583581
```js
@@ -607,7 +605,8 @@ changes:
607605
* `expected` {any}
608606
* `message` {any}
609607

610-
Tests equality determined by the [`Object.is()`][] comparison.
608+
Tests strict equality between the `actual` and `expected` parameters as
609+
determined by the [SameValue Comparison][].
611610

612611
```js
613612
const assert = require('assert');
@@ -625,8 +624,8 @@ assert.strictEqual(1, '1');
625624
If the values are not strictly equal, an `AssertionError` is thrown with a
626625
`message` property set equal to the value of the `message` parameter. If the
627626
`message` parameter is undefined, a default error message is assigned. If the
628-
`message` parameter is an instance of an `Error` then it will be thrown instead
629-
of the `AssertionError`.
627+
`message` parameter is an instance of an [`Error`][] then it will be thrown
628+
instead of the `AssertionError`.
630629

631630
## assert.throws(block[, error][, message])
632631
<!-- YAML
@@ -712,9 +711,8 @@ assert.throws(myFunction, /missing foo/, 'did not throw with expected message');
712711
[`assert.throws()`]: #assert_assert_throws_block_error_message
713712
[Abstract Equality Comparison]: https://tc39.github.io/ecma262/#sec-abstract-equality-comparison
714713
[Object.prototype.toString()]: https://tc39.github.io/ecma262/#sec-object.prototype.tostring
715-
[SameValueZero]: https://tc39.github.io/ecma262/#sec-samevaluezero
714+
[SameValue Comparison]: https://tc39.github.io/ecma262/#sec-samevalue
716715
[Strict Equality Comparison]: https://tc39.github.io/ecma262/#sec-strict-equality-comparison
717-
[caveats]: #assert_caveats
718716
[enumerable "own" properties]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties
719717
[mdn-equality-guide]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
720718
[prototype-spec]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots

0 commit comments

Comments
 (0)