@@ -43,13 +43,17 @@ changes:
43
43
-->
44
44
45
45
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:
53
57
54
58
``` js
55
59
// WARNING: This does not throw an AssertionError!
@@ -113,17 +117,20 @@ changes:
113
117
description: Handle non-`Uint8Array` typed arrays correctly.
114
118
-->
115
119
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.
119
126
120
127
``` js
121
128
const assert = require (' assert' );
122
129
123
- assert .deepEqual ({a: 1 }, {a: ' 1' });
130
+ assert .deepEqual ({a: 1 }, {a: ' 1' });
124
131
// OK, because 1 == '1'
125
132
126
- assert .deepStrictEqual ({a: 1 }, {a: ' 1' });
133
+ assert .deepStrictEqual ({a: 1 }, {a: ' 1' });
127
134
// AssertionError: { a: 1 } deepStrictEqual { a: '1' }
128
135
// because 1 !== '1' using strict equality
129
136
```
@@ -200,7 +207,7 @@ added: v0.1.21
200
207
-->
201
208
202
209
Tests shallow, coercive equality between the ` actual ` and ` expected ` parameters
203
- using the equal comparison operator ( ` == ` ).
210
+ using the [ Abstract Equality Comparison ] [ ] ( ` == ` ).
204
211
205
212
``` js
206
213
const assert = require (' assert' );
@@ -330,7 +337,7 @@ the `message` parameter is undefined, a default error message is assigned.
330
337
added: v0.1.21
331
338
-->
332
339
333
- Tests shallow, coercive inequality with the not equal comparison operator
340
+ Tests shallow, coercive inequality with the [ Abstract Equality Comparison ] [ ]
334
341
( ` != ` ).
335
342
336
343
``` js
@@ -355,7 +362,7 @@ parameter is undefined, a default error message is assigned.
355
362
added: v0.1.21
356
363
-->
357
364
358
- Tests strict inequality as determined by the strict not equal operator
365
+ Tests strict inequality as determined by the [ Strict Equality Comparison ] [ ]
359
366
( ` !== ` ).
360
367
361
368
``` js
@@ -407,7 +414,8 @@ assert.ok(false, 'it\'s false');
407
414
added: v0.1.21
408
415
-->
409
416
410
- Tests strict equality as determined by the strict equality operator ( ` === ` ).
417
+ Tests strict equality as determined by the [ Strict Equality Comparison] [ ]
418
+ ( ` === ` ).
411
419
412
420
``` js
413
421
const assert = require (' assert' );
@@ -493,10 +501,43 @@ assert.throws(myFunction, 'missing foo', 'did not throw with expected message');
493
501
assert .throws (myFunction, / missing foo/ , ' did not throw with expected message' );
494
502
```
495
503
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
+
496
530
[ `assert.deepEqual()` ] : #assert_assert_deepequal_actual_expected_message
497
531
[ `assert.deepStrictEqual()` ] : #assert_assert_deepstrictequal_actual_expected_message
498
532
[ `assert.ok()` ] : #assert_assert_ok_value_message
499
533
[ `assert.throws()` ] : #assert_assert_throws_block_error_message
500
534
[ `Error` ] : errors.html#errors_class_error
501
535
[ `RegExp` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
502
536
[ `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