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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Ruben Bridgewater 2017-11-13 20:34:16 -02:00 committed by James M Snell
parent 982c67419b
commit 493340f56e
7 changed files with 28 additions and 48 deletions

View File

@ -7,6 +7,9 @@
The `assert` module provides a simple set of assertion tests that can be used to The `assert` module provides a simple set of assertion tests that can be used to
test invariants. test invariants.
For more information about the used equality comparisons see
[MDN's guide on equality comparisons and sameness][mdn-equality-guide].
## assert(value[, message]) ## assert(value[, message])
<!-- YAML <!-- YAML
added: v0.5.9 added: v0.5.9
@ -531,13 +534,16 @@ parameter is an instance of an `Error` then it will be thrown instead of the
## assert.notStrictEqual(actual, expected[, message]) ## assert.notStrictEqual(actual, expected[, message])
<!-- YAML <!-- YAML
added: v0.1.21 added: v0.1.21
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/17003
description: Used comparison changed from Strict Equality to `Object.is()`
--> -->
* `actual` {any} * `actual` {any}
* `expected` {any} * `expected` {any}
* `message` {any} * `message` {any}
Tests strict inequality as determined by the [Strict Equality Comparison][] Tests equality determined by the [`Object.is()`][] comparison.
( `!==` ).
```js ```js
const assert = require('assert'); const assert = require('assert');
@ -546,7 +552,7 @@ assert.notStrictEqual(1, 2);
// OK // OK
assert.notStrictEqual(1, 1); assert.notStrictEqual(1, 1);
// AssertionError: 1 !== 1 // AssertionError: 1 notStrictEqual 1
assert.notStrictEqual(1, '1'); assert.notStrictEqual(1, '1');
// OK // OK
@ -592,25 +598,28 @@ assert.ok(false, 'it\'s false');
## assert.strictEqual(actual, expected[, message]) ## assert.strictEqual(actual, expected[, message])
<!-- YAML <!-- YAML
added: v0.1.21 added: v0.1.21
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/17003
description: Used comparison changed from Strict Equality to `Object.is()`
--> -->
* `actual` {any} * `actual` {any}
* `expected` {any} * `expected` {any}
* `message` {any} * `message` {any}
Tests strict equality as determined by the [Strict Equality Comparison][] Tests equality determined by the [`Object.is()`][] comparison.
( `===` ).
```js ```js
const assert = require('assert'); const assert = require('assert');
assert.strictEqual(1, 2); assert.strictEqual(1, 2);
// AssertionError: 1 === 2 // AssertionError: 1 strictEqual 2
assert.strictEqual(1, 1); assert.strictEqual(1, 1);
// OK // OK
assert.strictEqual(1, '1'); assert.strictEqual(1, '1');
// AssertionError: 1 === '1' // AssertionError: 1 strictEqual '1'
``` ```
If the values are not strictly equal, an `AssertionError` is thrown with a 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');
assert.throws(myFunction, /missing foo/, 'did not throw with expected message'); assert.throws(myFunction, /missing foo/, 'did not throw with expected message');
``` ```
## Caveats
For the following cases, consider using ES2015 [`Object.is()`][],
which uses the [SameValueZero][] comparison.
```js
const a = 0;
const b = -a;
assert.notStrictEqual(a, b);
// AssertionError: 0 !== -0
// Strict Equality Comparison doesn't distinguish between -0 and +0...
assert(!Object.is(a, b));
// but Object.is() does!
const str1 = 'foo';
const str2 = 'foo';
assert.strictEqual(str1 / 1, str2 / 1);
// AssertionError: NaN === NaN
// Strict Equality Comparison can't be used to check NaN...
assert(Object.is(str1 / 1, str2 / 1));
// but Object.is() can!
```
For more information, see
[MDN's guide on equality comparisons and sameness][mdn-equality-guide].
[`Error.captureStackTrace`]: errors.html#errors_error_capturestacktrace_targetobject_constructoropt [`Error.captureStackTrace`]: errors.html#errors_error_capturestacktrace_targetobject_constructoropt
[`Map`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map [`Map`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map
[`Object.is()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is [`Object.is()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is

View File

@ -124,18 +124,15 @@ function notDeepStrictEqual(actual, expected, message) {
} }
} }
// The strict equality assertion tests strict equality, as determined by ===.
assert.strictEqual = function strictEqual(actual, expected, message) { assert.strictEqual = function strictEqual(actual, expected, message) {
if (actual !== expected) { if (!Object.is(actual, expected)) {
innerFail(actual, expected, message, '===', strictEqual); innerFail(actual, expected, message, 'strictEqual', strictEqual);
} }
}; };
// The strict non-equality assertion tests for strict inequality, as
// determined by !==.
assert.notStrictEqual = function notStrictEqual(actual, expected, message) { assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
if (actual === expected) { if (Object.is(actual, expected)) {
innerFail(actual, expected, message, '!==', notStrictEqual); innerFail(actual, expected, message, 'notStrictEqual', notStrictEqual);
} }
}; };

View File

@ -27,7 +27,7 @@ assert.strictEqual(byteResult[2], 6);
const doubleResult = test_typedarray.Multiply(doubleArray, -3); const doubleResult = test_typedarray.Multiply(doubleArray, -3);
assert.ok(doubleResult instanceof Float64Array); assert.ok(doubleResult instanceof Float64Array);
assert.strictEqual(doubleResult.length, 3); assert.strictEqual(doubleResult.length, 3);
assert.strictEqual(doubleResult[0], 0); assert.strictEqual(doubleResult[0], -0);
assert.strictEqual(Math.round(10 * doubleResult[1]) / 10, -3.3); assert.strictEqual(Math.round(10 * doubleResult[1]) / 10, -3.3);
assert.strictEqual(Math.round(10 * doubleResult[2]) / 10, -6.6); assert.strictEqual(Math.round(10 * doubleResult[2]) / 10, -6.6);

View File

@ -3,7 +3,7 @@ assert.js:*
throw new errors.AssertionError({ throw new errors.AssertionError({
^ ^
AssertionError [ERR_ASSERTION]: 1 === 2 AssertionError [ERR_ASSERTION]: 1 strictEqual 2
at Object.<anonymous> (*test*message*error_exit.js:*:*) at Object.<anonymous> (*test*message*error_exit.js:*:*)
at Module._compile (module.js:*:*) at Module._compile (module.js:*:*)
at Object.Module._extensions..js (module.js:*:*) at Object.Module._extensions..js (module.js:*:*)

View File

@ -586,7 +586,7 @@ function testAssertionMessage(actual, expected) {
assert.strictEqual(actual, ''); assert.strictEqual(actual, '');
} catch (e) { } catch (e) {
assert.strictEqual(e.message, assert.strictEqual(e.message,
[expected, '===', '\'\''].join(' ')); [expected, 'strictEqual', '\'\''].join(' '));
assert.ok(e.generatedMessage, 'Message not marked as generated'); assert.ok(e.generatedMessage, 'Message not marked as generated');
} }
} }
@ -633,7 +633,7 @@ testAssertionMessage({ a: NaN, b: Infinity, c: -Infinity },
try { try {
assert.strictEqual(1, 2); assert.strictEqual(1, 2);
} catch (e) { } catch (e) {
assert.strictEqual(e.message.split('\n')[0], '1 === 2'); assert.strictEqual(e.message.split('\n')[0], '1 strictEqual 2');
assert.ok(e.generatedMessage, 'Message not marked as generated'); assert.ok(e.generatedMessage, 'Message not marked as generated');
} }
@ -727,7 +727,7 @@ assert.throws(() => {
assert.strictEqual('A'.repeat(1000), ''); assert.strictEqual('A'.repeat(1000), '');
}, common.expectsError({ }, common.expectsError({
code: 'ERR_ASSERTION', code: 'ERR_ASSERTION',
message: new RegExp(`^'${'A'.repeat(127)} === ''$`) })); message: new RegExp(`^'${'A'.repeat(127)} strictEqual ''$`) }));
{ {
// bad args to AssertionError constructor should throw TypeError // bad args to AssertionError constructor should throw TypeError
@ -749,6 +749,6 @@ common.expectsError(
{ {
code: 'ERR_ASSERTION', code: 'ERR_ASSERTION',
type: assert.AssertionError, type: assert.AssertionError,
message: /^'Error: foo' === 'Error: foobar'$/ message: /^'Error: foo' strictEqual 'Error: foobar'$/
} }
); );

View File

@ -112,7 +112,7 @@ function test(clazz) {
buffer[7] = 0x80; buffer[7] = 0x80;
assert.strictEqual(6.3e-322, buffer.readDoubleBE(0)); assert.strictEqual(6.3e-322, buffer.readDoubleBE(0));
assert.strictEqual(0, buffer.readDoubleLE(0)); assert.strictEqual(-0, buffer.readDoubleLE(0));
assert.strictEqual(true, 1 / buffer.readDoubleLE(0) < 0); assert.strictEqual(true, 1 / buffer.readDoubleLE(0) < 0);
buffer[6] = 0xf0; buffer[6] = 0xf0;

View File

@ -70,7 +70,7 @@ function test(clazz) {
buffer[3] = 0x80; buffer[3] = 0x80;
assert.strictEqual(1.793662034335766e-43, buffer.readFloatBE(0)); assert.strictEqual(1.793662034335766e-43, buffer.readFloatBE(0));
assert.strictEqual(0, buffer.readFloatLE(0)); assert.strictEqual(-0, buffer.readFloatLE(0));
assert.strictEqual(true, 1 / buffer.readFloatLE(0) < 0); assert.strictEqual(true, 1 / buffer.readFloatLE(0) < 0);
buffer[0] = 0; buffer[0] = 0;