Skip to content

Commit

Permalink
assert: use Object.is comparison in .strictEqual
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
BridgeAR authored and jasnell committed Nov 22, 2017
1 parent 982c674 commit 493340f
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 48 deletions.
49 changes: 16 additions & 33 deletions doc/api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
The `assert` module provides a simple set of assertion tests that can be used to
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])
<!-- YAML
added: v0.5.9
Expand Down Expand Up @@ -531,13 +534,16 @@ parameter is an instance of an `Error` then it will be thrown instead of the
## assert.notStrictEqual(actual, expected[, message])
<!-- YAML
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}
* `expected` {any}
* `message` {any}

Tests strict inequality as determined by the [Strict Equality Comparison][]
( `!==` ).
Tests equality determined by the [`Object.is()`][] comparison.

```js
const assert = require('assert');
Expand All @@ -546,7 +552,7 @@ assert.notStrictEqual(1, 2);
// OK

assert.notStrictEqual(1, 1);
// AssertionError: 1 !== 1
// AssertionError: 1 notStrictEqual 1

assert.notStrictEqual(1, '1');
// OK
Expand Down Expand Up @@ -592,25 +598,28 @@ assert.ok(false, 'it\'s false');
## assert.strictEqual(actual, expected[, message])
<!-- YAML
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}
* `expected` {any}
* `message` {any}

Tests strict equality as determined by the [Strict Equality Comparison][]
( `===` ).
Tests equality determined by the [`Object.is()`][] comparison.

```js
const assert = require('assert');

assert.strictEqual(1, 2);
// AssertionError: 1 === 2
// AssertionError: 1 strictEqual 2

assert.strictEqual(1, 1);
// OK

assert.strictEqual(1, '1');
// AssertionError: 1 === '1'
// AssertionError: 1 strictEqual '1'
```

If the values are not strictly equal, an `AssertionError` is thrown with a
Expand Down Expand Up @@ -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');
```

## 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
[`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
Expand Down
11 changes: 4 additions & 7 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
if (actual !== expected) {
innerFail(actual, expected, message, '===', strictEqual);
if (!Object.is(actual, expected)) {
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) {
if (actual === expected) {
innerFail(actual, expected, message, '!==', notStrictEqual);
if (Object.is(actual, expected)) {
innerFail(actual, expected, message, 'notStrictEqual', notStrictEqual);
}
};

Expand Down
2 changes: 1 addition & 1 deletion test/addons-napi/test_typedarray/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ assert.strictEqual(byteResult[2], 6);
const doubleResult = test_typedarray.Multiply(doubleArray, -3);
assert.ok(doubleResult instanceof Float64Array);
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[2]) / 10, -6.6);

Expand Down
2 changes: 1 addition & 1 deletion test/message/error_exit.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ assert.js:*
throw new errors.AssertionError({
^

AssertionError [ERR_ASSERTION]: 1 === 2
AssertionError [ERR_ASSERTION]: 1 strictEqual 2
at Object.<anonymous> (*test*message*error_exit.js:*:*)
at Module._compile (module.js:*:*)
at Object.Module._extensions..js (module.js:*:*)
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ function testAssertionMessage(actual, expected) {
assert.strictEqual(actual, '');
} catch (e) {
assert.strictEqual(e.message,
[expected, '===', '\'\''].join(' '));
[expected, 'strictEqual', '\'\''].join(' '));
assert.ok(e.generatedMessage, 'Message not marked as generated');
}
}
Expand Down Expand Up @@ -633,7 +633,7 @@ testAssertionMessage({ a: NaN, b: Infinity, c: -Infinity },
try {
assert.strictEqual(1, 2);
} 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');
}

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

{
// bad args to AssertionError constructor should throw TypeError
Expand All @@ -749,6 +749,6 @@ common.expectsError(
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: /^'Error: foo' === 'Error: foobar'$/
message: /^'Error: foo' strictEqual 'Error: foobar'$/
}
);
2 changes: 1 addition & 1 deletion test/parallel/test-readdouble.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function test(clazz) {

buffer[7] = 0x80;
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);

buffer[6] = 0xf0;
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-readfloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function test(clazz) {

buffer[3] = 0x80;
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);

buffer[0] = 0;
Expand Down

0 comments on commit 493340f

Please sign in to comment.