Skip to content

Commit 493340f

Browse files
BridgeARjasnell
authored andcommitted
assert: use Object.is comparison in .strictEqual
This aligns assert.strictEqual and assert.notStrictEqual with assert.deepStrictEqual to use the Object.is() comparison instead of strict equality. PR-URL: #17003 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 982c674 commit 493340f

File tree

7 files changed

+28
-48
lines changed

7 files changed

+28
-48
lines changed

doc/api/assert.md

+16-33
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
The `assert` module provides a simple set of assertion tests that can be used to
88
test invariants.
99

10+
For more information about the used equality comparisons see
11+
[MDN's guide on equality comparisons and sameness][mdn-equality-guide].
12+
1013
## assert(value[, message])
1114
<!-- YAML
1215
added: v0.5.9
@@ -531,13 +534,16 @@ parameter is an instance of an `Error` then it will be thrown instead of the
531534
## assert.notStrictEqual(actual, expected[, message])
532535
<!-- YAML
533536
added: v0.1.21
537+
changes:
538+
- version: REPLACEME
539+
pr-url: https://github.com/nodejs/node/pull/17003
540+
description: Used comparison changed from Strict Equality to `Object.is()`
534541
-->
535542
* `actual` {any}
536543
* `expected` {any}
537544
* `message` {any}
538545

539-
Tests strict inequality as determined by the [Strict Equality Comparison][]
540-
( `!==` ).
546+
Tests equality determined by the [`Object.is()`][] comparison.
541547

542548
```js
543549
const assert = require('assert');
@@ -546,7 +552,7 @@ assert.notStrictEqual(1, 2);
546552
// OK
547553

548554
assert.notStrictEqual(1, 1);
549-
// AssertionError: 1 !== 1
555+
// AssertionError: 1 notStrictEqual 1
550556

551557
assert.notStrictEqual(1, '1');
552558
// OK
@@ -592,25 +598,28 @@ assert.ok(false, 'it\'s false');
592598
## assert.strictEqual(actual, expected[, message])
593599
<!-- YAML
594600
added: v0.1.21
601+
changes:
602+
- version: REPLACEME
603+
pr-url: https://github.com/nodejs/node/pull/17003
604+
description: Used comparison changed from Strict Equality to `Object.is()`
595605
-->
596606
* `actual` {any}
597607
* `expected` {any}
598608
* `message` {any}
599609

600-
Tests strict equality as determined by the [Strict Equality Comparison][]
601-
( `===` ).
610+
Tests equality determined by the [`Object.is()`][] comparison.
602611

603612
```js
604613
const assert = require('assert');
605614

606615
assert.strictEqual(1, 2);
607-
// AssertionError: 1 === 2
616+
// AssertionError: 1 strictEqual 2
608617

609618
assert.strictEqual(1, 1);
610619
// OK
611620

612621
assert.strictEqual(1, '1');
613-
// AssertionError: 1 === '1'
622+
// AssertionError: 1 strictEqual '1'
614623
```
615624

616625
If the values are not strictly equal, an `AssertionError` is thrown with a
@@ -690,32 +699,6 @@ assert.throws(myFunction, 'missing foo', 'did not throw with expected message');
690699
assert.throws(myFunction, /missing foo/, 'did not throw with expected message');
691700
```
692701

693-
## Caveats
694-
695-
For the following cases, consider using ES2015 [`Object.is()`][],
696-
which uses the [SameValueZero][] comparison.
697-
698-
```js
699-
const a = 0;
700-
const b = -a;
701-
assert.notStrictEqual(a, b);
702-
// AssertionError: 0 !== -0
703-
// Strict Equality Comparison doesn't distinguish between -0 and +0...
704-
assert(!Object.is(a, b));
705-
// but Object.is() does!
706-
707-
const str1 = 'foo';
708-
const str2 = 'foo';
709-
assert.strictEqual(str1 / 1, str2 / 1);
710-
// AssertionError: NaN === NaN
711-
// Strict Equality Comparison can't be used to check NaN...
712-
assert(Object.is(str1 / 1, str2 / 1));
713-
// but Object.is() can!
714-
```
715-
716-
For more information, see
717-
[MDN's guide on equality comparisons and sameness][mdn-equality-guide].
718-
719702
[`Error.captureStackTrace`]: errors.html#errors_error_capturestacktrace_targetobject_constructoropt
720703
[`Map`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map
721704
[`Object.is()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is

lib/assert.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,15 @@ function notDeepStrictEqual(actual, expected, message) {
124124
}
125125
}
126126

127-
// The strict equality assertion tests strict equality, as determined by ===.
128127
assert.strictEqual = function strictEqual(actual, expected, message) {
129-
if (actual !== expected) {
130-
innerFail(actual, expected, message, '===', strictEqual);
128+
if (!Object.is(actual, expected)) {
129+
innerFail(actual, expected, message, 'strictEqual', strictEqual);
131130
}
132131
};
133132

134-
// The strict non-equality assertion tests for strict inequality, as
135-
// determined by !==.
136133
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
137-
if (actual === expected) {
138-
innerFail(actual, expected, message, '!==', notStrictEqual);
134+
if (Object.is(actual, expected)) {
135+
innerFail(actual, expected, message, 'notStrictEqual', notStrictEqual);
139136
}
140137
};
141138

test/addons-napi/test_typedarray/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ assert.strictEqual(byteResult[2], 6);
2727
const doubleResult = test_typedarray.Multiply(doubleArray, -3);
2828
assert.ok(doubleResult instanceof Float64Array);
2929
assert.strictEqual(doubleResult.length, 3);
30-
assert.strictEqual(doubleResult[0], 0);
30+
assert.strictEqual(doubleResult[0], -0);
3131
assert.strictEqual(Math.round(10 * doubleResult[1]) / 10, -3.3);
3232
assert.strictEqual(Math.round(10 * doubleResult[2]) / 10, -6.6);
3333

test/message/error_exit.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ assert.js:*
33
throw new errors.AssertionError({
44
^
55

6-
AssertionError [ERR_ASSERTION]: 1 === 2
6+
AssertionError [ERR_ASSERTION]: 1 strictEqual 2
77
at Object.<anonymous> (*test*message*error_exit.js:*:*)
88
at Module._compile (module.js:*:*)
99
at Object.Module._extensions..js (module.js:*:*)

test/parallel/test-assert.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ function testAssertionMessage(actual, expected) {
586586
assert.strictEqual(actual, '');
587587
} catch (e) {
588588
assert.strictEqual(e.message,
589-
[expected, '===', '\'\''].join(' '));
589+
[expected, 'strictEqual', '\'\''].join(' '));
590590
assert.ok(e.generatedMessage, 'Message not marked as generated');
591591
}
592592
}
@@ -633,7 +633,7 @@ testAssertionMessage({ a: NaN, b: Infinity, c: -Infinity },
633633
try {
634634
assert.strictEqual(1, 2);
635635
} catch (e) {
636-
assert.strictEqual(e.message.split('\n')[0], '1 === 2');
636+
assert.strictEqual(e.message.split('\n')[0], '1 strictEqual 2');
637637
assert.ok(e.generatedMessage, 'Message not marked as generated');
638638
}
639639

@@ -727,7 +727,7 @@ assert.throws(() => {
727727
assert.strictEqual('A'.repeat(1000), '');
728728
}, common.expectsError({
729729
code: 'ERR_ASSERTION',
730-
message: new RegExp(`^'${'A'.repeat(127)} === ''$`) }));
730+
message: new RegExp(`^'${'A'.repeat(127)} strictEqual ''$`) }));
731731

732732
{
733733
// bad args to AssertionError constructor should throw TypeError
@@ -749,6 +749,6 @@ common.expectsError(
749749
{
750750
code: 'ERR_ASSERTION',
751751
type: assert.AssertionError,
752-
message: /^'Error: foo' === 'Error: foobar'$/
752+
message: /^'Error: foo' strictEqual 'Error: foobar'$/
753753
}
754754
);

test/parallel/test-readdouble.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function test(clazz) {
112112

113113
buffer[7] = 0x80;
114114
assert.strictEqual(6.3e-322, buffer.readDoubleBE(0));
115-
assert.strictEqual(0, buffer.readDoubleLE(0));
115+
assert.strictEqual(-0, buffer.readDoubleLE(0));
116116
assert.strictEqual(true, 1 / buffer.readDoubleLE(0) < 0);
117117

118118
buffer[6] = 0xf0;

test/parallel/test-readfloat.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function test(clazz) {
7070

7171
buffer[3] = 0x80;
7272
assert.strictEqual(1.793662034335766e-43, buffer.readFloatBE(0));
73-
assert.strictEqual(0, buffer.readFloatLE(0));
73+
assert.strictEqual(-0, buffer.readFloatLE(0));
7474
assert.strictEqual(true, 1 / buffer.readFloatLE(0) < 0);
7575

7676
buffer[0] = 0;

0 commit comments

Comments
 (0)