7
7
The ` assert ` module provides a simple set of assertion tests that can be used to
8
8
test invariants.
9
9
10
+ For more information about the used equality comparisons see
11
+ [ MDN's guide on equality comparisons and sameness] [ mdn-equality-guide ] .
12
+
10
13
## assert(value[ , message] )
11
14
<!-- YAML
12
15
added: v0.5.9
@@ -531,13 +534,16 @@ parameter is an instance of an `Error` then it will be thrown instead of the
531
534
## assert.notStrictEqual(actual, expected[ , message] )
532
535
<!-- YAML
533
536
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()`
534
541
-->
535
542
* ` actual ` {any}
536
543
* ` expected ` {any}
537
544
* ` message ` {any}
538
545
539
- Tests strict inequality as determined by the [ Strict Equality Comparison] [ ]
540
- ( ` !== ` ).
546
+ Tests equality determined by the [ ` Object.is() ` ] [ ] comparison.
541
547
542
548
``` js
543
549
const assert = require (' assert' );
@@ -546,7 +552,7 @@ assert.notStrictEqual(1, 2);
546
552
// OK
547
553
548
554
assert .notStrictEqual (1 , 1 );
549
- // AssertionError: 1 !== 1
555
+ // AssertionError: 1 notStrictEqual 1
550
556
551
557
assert .notStrictEqual (1 , ' 1' );
552
558
// OK
@@ -592,25 +598,28 @@ assert.ok(false, 'it\'s false');
592
598
## assert.strictEqual(actual, expected[ , message] )
593
599
<!-- YAML
594
600
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()`
595
605
-->
596
606
* ` actual ` {any}
597
607
* ` expected ` {any}
598
608
* ` message ` {any}
599
609
600
- Tests strict equality as determined by the [ Strict Equality Comparison] [ ]
601
- ( ` === ` ).
610
+ Tests equality determined by the [ ` Object.is() ` ] [ ] comparison.
602
611
603
612
``` js
604
613
const assert = require (' assert' );
605
614
606
615
assert .strictEqual (1 , 2 );
607
- // AssertionError: 1 === 2
616
+ // AssertionError: 1 strictEqual 2
608
617
609
618
assert .strictEqual (1 , 1 );
610
619
// OK
611
620
612
621
assert .strictEqual (1 , ' 1' );
613
- // AssertionError: 1 === '1'
622
+ // AssertionError: 1 strictEqual '1'
614
623
```
615
624
616
625
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');
690
699
assert .throws (myFunction, / missing foo/ , ' did not throw with expected message' );
691
700
```
692
701
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
-
719
702
[ `Error.captureStackTrace` ] : errors.html#errors_error_capturestacktrace_targetobject_constructoropt
720
703
[ `Map` ] : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map
721
704
[ `Object.is()` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
0 commit comments