Skip to content

Commit ea2e636

Browse files
committed
assert: use SameValueZero in deepStrictEqual
Comparing NaN will not throw anymore. PR-URL: #15036 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 1789dcf commit ea2e636

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

doc/api/assert.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ parameter is undefined, a default error message is assigned.
107107
<!-- YAML
108108
added: v1.2.0
109109
changes:
110+
- version: REPLACEME
111+
pr-url: https://github.com/nodejs/node/pull/15036
112+
description: NaN is now compared using the [SameValueZero][] comparison.
110113
- version: REPLACEME
111114
pr-url: https://github.com/nodejs/node/pull/15001
112115
description: Error names and messages are now properly compared
@@ -129,9 +132,10 @@ changes:
129132

130133
Generally identical to `assert.deepEqual()` with three exceptions:
131134

132-
1. Primitive values are compared using the [Strict Equality Comparison][]
133-
( `===` ). Set values and Map keys are compared using the [SameValueZero][]
134-
comparison. (Which means they are free of the [caveats][]).
135+
1. Primitive values besides `NaN` are compared using the [Strict Equality
136+
Comparison][] ( `===` ). Set and Map values, Map keys and `NaN` are compared
137+
using the [SameValueZero][] comparison (which means they are free of the
138+
[caveats][]).
135139
2. [`[[Prototype]]`][prototype-spec] of objects are compared using
136140
the [Strict Equality Comparison][] too.
137141
3. [Type tags][Object.prototype.toString()] of objects should be the same.
@@ -164,6 +168,8 @@ assert.deepEqual(date, fakeDate);
164168
assert.deepStrictEqual(date, fakeDate);
165169
// AssertionError: 2017-03-11T14:25:31.849Z deepStrictEqual Date {}
166170
// Different type tags
171+
assert.deepStrictEqual(NaN, NaN);
172+
// OK, because of the SameValueZero comparison
167173
```
168174

169175
If the values are not equal, an `AssertionError` is thrown with a `message`
@@ -412,6 +418,9 @@ parameter is undefined, a default error message is assigned.
412418
<!-- YAML
413419
added: v1.2.0
414420
changes:
421+
- version: REPLACEME
422+
pr-url: https://github.com/nodejs/node/pull/15036
423+
description: NaN is now compared using the [SameValueZero][] comparison.
415424
- version: REPLACEME
416425
pr-url: https://github.com/nodejs/node/pull/15001
417426
description: Error names and messages are now properly compared

lib/assert.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,11 @@ function isObjectOrArrayTag(tag) {
167167
// a) The same built-in type tags
168168
// b) The same prototypes.
169169
function strictDeepEqual(actual, expected) {
170-
if (actual === null || expected === null ||
171-
typeof actual !== 'object' || typeof expected !== 'object') {
170+
if (typeof actual !== 'object') {
171+
return typeof actual === 'number' && Number.isNaN(actual) &&
172+
Number.isNaN(expected);
173+
}
174+
if (typeof expected !== 'object' || actual === null || expected === null) {
172175
return false;
173176
}
174177
const actualTag = objectToString(actual);

test/parallel/test-assert-deep.js

+8
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ assertOnlyDeepEqual(
466466
assertDeepAndStrictEqual(m3, m4);
467467
}
468468

469+
// Handle sparse arrays
469470
assertDeepAndStrictEqual([1, , , 3], [1, , , 3]);
470471
assertOnlyDeepEqual([1, , , 3], [1, , , 3, , , ]);
471472

@@ -481,4 +482,11 @@ assertOnlyDeepEqual([1, , , 3], [1, , , 3, , , ]);
481482
assertOnlyDeepEqual(err1, {}, assert.AssertionError);
482483
}
483484

485+
// Handle NaN
486+
assert.throws(() => { assert.deepEqual(NaN, NaN); }, assert.AssertionError);
487+
assert.doesNotThrow(() => { assert.deepStrictEqual(NaN, NaN); });
488+
assert.doesNotThrow(() => { assert.deepStrictEqual({ a: NaN }, { a: NaN }); });
489+
assert.doesNotThrow(
490+
() => { assert.deepStrictEqual([ 1, 2, NaN, 4 ], [ 1, 2, NaN, 4 ]); });
491+
484492
/* eslint-enable */

0 commit comments

Comments
 (0)