Skip to content

Commit 3d8379a

Browse files
committed
doc: improve assert.md regarding ECMAScript terms
Use the term "Abstract Equality Comparison" and "Strict Equality Comparison" from ECMAScript specification to refer to the operations done by `==` and `===`, instead of "equal comparison operator" and "strict equality operator". Clarify that deep strict comparisons checks `[[Prototype]]` property, instead of the vague "object prototypes". Suggest using `Object.is()` to avoid the caveats of +0, -0 and NaN. Add a MDN link explaining what enumerable "own" properties are. PR-URL: #11128 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent 562cf5a commit 3d8379a

File tree

1 file changed

+57
-16
lines changed

1 file changed

+57
-16
lines changed

doc/api/assert.md

+57-16
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ changes:
4343
-->
4444

4545
Tests for deep equality between the `actual` and `expected` parameters.
46-
Primitive values are compared with the equal comparison operator ( `==` ).
47-
48-
Only enumerable "own" properties are considered. The `deepEqual()`
49-
implementation does not test object prototypes, attached symbols, or
50-
non-enumerable properties. This can lead to some potentially surprising
51-
results. For example, the following example does not throw an `AssertionError`
52-
because the properties on the [`Error`][] object are non-enumerable:
46+
Primitive values are compared with the [Abstract Equality Comparison][]
47+
( `==` ).
48+
49+
Only [enumerable "own" properties][] are considered. The
50+
[`assert.deepEqual()`][] implementation does not test the
51+
[`[[Prototype]]`][prototype-spec] of objects, attached symbols, or
52+
non-enumerable properties — for such checks, consider using
53+
[assert.deepStrictEqual()][] instead. This can lead to some
54+
potentially surprising results. For example, the following example does not
55+
throw an `AssertionError` because the properties on the [`Error`][] object are
56+
not enumerable:
5357

5458
```js
5559
// WARNING: This does not throw an AssertionError!
@@ -113,17 +117,20 @@ changes:
113117
description: Handle non-`Uint8Array` typed arrays correctly.
114118
-->
115119

116-
Generally identical to `assert.deepEqual()` with two exceptions. First,
117-
primitive values are compared using the strict equality operator ( `===` ).
118-
Second, object comparisons include a strict equality check of their prototypes.
120+
Generally identical to `assert.deepEqual()` with two exceptions:
121+
122+
1. Primitive values are compared using the [Strict Equality Comparison][]
123+
( `===` ).
124+
2. [`[[Prototype]]`][prototype-spec] of objects are compared using
125+
the [Strict Equality Comparison][] too.
119126

120127
```js
121128
const assert = require('assert');
122129

123-
assert.deepEqual({a:1}, {a:'1'});
130+
assert.deepEqual({a: 1}, {a: '1'});
124131
// OK, because 1 == '1'
125132

126-
assert.deepStrictEqual({a:1}, {a:'1'});
133+
assert.deepStrictEqual({a: 1}, {a: '1'});
127134
// AssertionError: { a: 1 } deepStrictEqual { a: '1' }
128135
// because 1 !== '1' using strict equality
129136
```
@@ -200,7 +207,7 @@ added: v0.1.21
200207
-->
201208

202209
Tests shallow, coercive equality between the `actual` and `expected` parameters
203-
using the equal comparison operator ( `==` ).
210+
using the [Abstract Equality Comparison][] ( `==` ).
204211

205212
```js
206213
const assert = require('assert');
@@ -330,7 +337,7 @@ the `message` parameter is undefined, a default error message is assigned.
330337
added: v0.1.21
331338
-->
332339

333-
Tests shallow, coercive inequality with the not equal comparison operator
340+
Tests shallow, coercive inequality with the [Abstract Equality Comparison][]
334341
( `!=` ).
335342

336343
```js
@@ -355,7 +362,7 @@ parameter is undefined, a default error message is assigned.
355362
added: v0.1.21
356363
-->
357364

358-
Tests strict inequality as determined by the strict not equal operator
365+
Tests strict inequality as determined by the [Strict Equality Comparison][]
359366
( `!==` ).
360367

361368
```js
@@ -407,7 +414,8 @@ assert.ok(false, 'it\'s false');
407414
added: v0.1.21
408415
-->
409416

410-
Tests strict equality as determined by the strict equality operator ( `===` ).
417+
Tests strict equality as determined by the [Strict Equality Comparison][]
418+
( `===` ).
411419

412420
```js
413421
const assert = require('assert');
@@ -493,10 +501,43 @@ assert.throws(myFunction, 'missing foo', 'did not throw with expected message');
493501
assert.throws(myFunction, /missing foo/, 'did not throw with expected message');
494502
```
495503

504+
## Caveats
505+
506+
For the following cases, consider using ES2015 [`Object.is()`][],
507+
which uses the [SameValueZero][] comparison.
508+
509+
```js
510+
const a = 0;
511+
const b = -a;
512+
assert.notStrictEqual(a, b);
513+
// AssertionError: 0 !== -0
514+
// Strict Equality Comparison doesn't distinguish between -0 and +0...
515+
assert(!Object.is(a, b));
516+
// but Object.is() does!
517+
518+
const str1 = "foo";
519+
const str2 = "foo";
520+
assert.strictEqual(str1 / 1, str2 / 1);
521+
// AssertionError: NaN === NaN
522+
// Strict Equality Comparison can't be used to check NaN...
523+
assert(Object.is(str1 / 1, str2 / 1));
524+
// but Object.is() can!
525+
```
526+
527+
For more information, see
528+
[MDN's guide on equality comparisons and sameness][mdn-equality-guide].
529+
496530
[`assert.deepEqual()`]: #assert_assert_deepequal_actual_expected_message
497531
[`assert.deepStrictEqual()`]: #assert_assert_deepstrictequal_actual_expected_message
498532
[`assert.ok()`]: #assert_assert_ok_value_message
499533
[`assert.throws()`]: #assert_assert_throws_block_error_message
500534
[`Error`]: errors.html#errors_class_error
501535
[`RegExp`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
502536
[`TypeError`]: errors.html#errors_class_typeerror
537+
[Abstract Equality Comparison]: https://tc39.github.io/ecma262/#sec-abstract-equality-comparison
538+
[Strict Equality Comparison]: https://tc39.github.io/ecma262/#sec-strict-equality-comparison
539+
[`Object.is()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
540+
[SameValueZero]: https://tc39.github.io/ecma262/#sec-samevaluezero
541+
[prototype-spec]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots
542+
[mdn-equality-guide]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
543+
[enumerable "own" properties]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties

0 commit comments

Comments
 (0)