7
7
The ` assert ` module provides a simple set of assertion tests that can be used to
8
8
test invariants.
9
9
10
+ A ` strict ` and a ` legacy ` mode exist, while it is recommended to only use
11
+ [ ` strict mode ` ] [ ] .
12
+
13
+ For more information about the used equality comparisons see
14
+ [ MDN's guide on equality comparisons and sameness] [ mdn-equality-guide ] .
15
+
16
+ ## Strict mode
17
+ <!-- YAML
18
+ added: REPLACEME
19
+ changes:
20
+ - version: REPLACEME
21
+ pr-url: https://github.com/nodejs/node/pull/17002
22
+ description: Added strict mode to the assert module.
23
+ -->
24
+
25
+ When using the ` strict mode ` , any ` assert ` function will use the equality used in
26
+ the strict function mode. So [ ` assert.deepEqual() ` ] [ ] will, for example, work the
27
+ same as [ ` assert.deepStrictEqual() ` ] [ ] .
28
+
29
+ It can be accessed using:
30
+
31
+ ``` js
32
+ const assert = require (' assert' ).strict ;
33
+ ```
34
+
35
+ ## Legacy mode
36
+
37
+ > Stability: 0 - Deprecated: Use strict mode instead.
38
+
39
+ When accessing ` assert ` directly instead of using the ` strict ` property, the
40
+ [ Abstract Equality Comparison] [ ] will be used for any function without a
41
+ "strict" in its name (e.g. [ ` assert.deepEqual() ` ] [ ] ).
42
+
43
+ It can be accessed using:
44
+
45
+ ``` js
46
+ const assert = require (' assert' );
47
+ ```
48
+
49
+ It is recommended to use the [ ` strict mode ` ] [ ] instead as the
50
+ [ Abstract Equality Comparison] [ ] can often have surprising results. Especially
51
+ in case of [ ` assert.deepEqual() ` ] [ ] as the used comparison rules there are very
52
+ lax.
53
+
54
+ E.g.
55
+
56
+ ``` js
57
+ // WARNING: This does not throw an AssertionError!
58
+ assert .deepEqual (/ a/ gi , new Date ());
59
+ ```
60
+
10
61
## assert(value[ , message] )
11
62
<!-- YAML
12
63
added: v0.5.9
@@ -40,6 +91,14 @@ changes:
40
91
* ` expected ` {any}
41
92
* ` message ` {any}
42
93
94
+ ** Strict mode**
95
+
96
+ An alias of [ ` assert.deepStrictEqual() ` ] [ ] .
97
+
98
+ ** Legacy mode**
99
+
100
+ > Stability: 0 - Deprecated: Use [ ` assert.deepStrictEqual() ` ] [ ] instead.
101
+
43
102
Tests for deep equality between the ` actual ` and ` expected ` parameters.
44
103
Primitive values are compared with the [ Abstract Equality Comparison] [ ]
45
104
( ` == ` ).
@@ -146,7 +205,7 @@ are recursively evaluated also by the following rules.
146
205
[ ` Object.is() ` ] [ ] .
147
206
* [ Type tags] [ Object.prototype.toString() ] of objects should be the same.
148
207
* [ ` [[Prototype]] ` ] [ prototype-spec ] of objects are compared using
149
- the [ Strict Equality Comparison] [ ] too .
208
+ the [ Strict Equality Comparison] [ ] .
150
209
* Only [ enumerable "own" properties] [ ] are considered.
151
210
* [ ` Error ` ] [ ] names and messages are always compared, even if these are not
152
211
enumerable properties.
@@ -158,7 +217,7 @@ are recursively evaluated also by the following rules.
158
217
reference.
159
218
160
219
``` js
161
- const assert = require (' assert' );
220
+ const assert = require (' assert' ). strict ;
162
221
163
222
assert .deepStrictEqual ({ a: 1 }, { a: ' 1' });
164
223
// AssertionError: { a: 1 } deepStrictEqual { a: '1' }
@@ -278,6 +337,14 @@ added: v0.1.21
278
337
* ` expected ` {any}
279
338
* ` message ` {any}
280
339
340
+ ** Strict mode**
341
+
342
+ An alias of [ ` assert.strictEqual() ` ] [ ] .
343
+
344
+ ** Legacy mode**
345
+
346
+ > Stability: 0 - Deprecated: Use [ ` assert.strictEqual() ` ] [ ] instead.
347
+
281
348
Tests shallow, coercive equality between the ` actual ` and ` expected ` parameters
282
349
using the [ Abstract Equality Comparison] [ ] ( ` == ` ).
283
350
@@ -324,7 +391,7 @@ all stack frames above that function will be removed from stacktrace (see
324
391
` Failed ` will be used.
325
392
326
393
``` js
327
- const assert = require (' assert' );
394
+ const assert = require (' assert' ). strict ;
328
395
329
396
assert .fail (1 , 2 , undefined , ' >' );
330
397
// AssertionError [ERR_ASSERTION]: 1 > 2
@@ -375,7 +442,7 @@ Throws `value` if `value` is truthy. This is useful when testing the `error`
375
442
argument in callbacks.
376
443
377
444
``` js
378
- const assert = require (' assert' );
445
+ const assert = require (' assert' ). strict ;
379
446
380
447
assert .ifError (null );
381
448
// OK
@@ -413,6 +480,14 @@ changes:
413
480
* ` expected ` {any}
414
481
* ` message ` {any}
415
482
483
+ ** Strict mode**
484
+
485
+ An alias of [ ` assert.notDeepStrictEqual() ` ] [ ] .
486
+
487
+ ** Legacy mode**
488
+
489
+ > Stability: 0 - Deprecated: Use [ ` assert.notDeepStrictEqual() ` ] [ ] instead.
490
+
416
491
Tests for any deep inequality. Opposite of [ ` assert.deepEqual() ` ] [ ] .
417
492
418
493
``` js
@@ -489,7 +564,7 @@ changes:
489
564
Tests for deep strict inequality. Opposite of [ ` assert.deepStrictEqual() ` ] [ ] .
490
565
491
566
``` js
492
- const assert = require (' assert' );
567
+ const assert = require (' assert' ). strict ;
493
568
494
569
assert .notDeepStrictEqual ({ a: 1 }, { a: ' 1' });
495
570
// OK
@@ -509,6 +584,14 @@ added: v0.1.21
509
584
* ` expected ` {any}
510
585
* ` message ` {any}
511
586
587
+ ** Strict mode**
588
+
589
+ An alias of [ ` assert.notStrictEqual() ` ] [ ] .
590
+
591
+ ** Legacy mode**
592
+
593
+ > Stability: 0 - Deprecated: Use [ ` assert.notStrictEqual() ` ] [ ] instead.
594
+
512
595
Tests shallow, coercive inequality with the [ Abstract Equality Comparison] [ ]
513
596
( ` != ` ).
514
597
@@ -543,7 +626,7 @@ Tests strict inequality between the `actual` and `expected` parameters as
543
626
determined by the [ SameValue Comparison] [ ] .
544
627
545
628
``` js
546
- const assert = require (' assert' );
629
+ const assert = require (' assert' ). strict ;
547
630
548
631
assert .notStrictEqual (1 , 2 );
549
632
// OK
@@ -578,7 +661,7 @@ parameter is an instance of an [`Error`][] then it will be thrown instead of the
578
661
` AssertionError ` .
579
662
580
663
``` js
581
- const assert = require (' assert' );
664
+ const assert = require (' assert' ). strict ;
582
665
583
666
assert .ok (true );
584
667
// OK
@@ -604,7 +687,7 @@ Tests strict equality between the `actual` and `expected` parameters as
604
687
determined by the [ SameValue Comparison] [ ] .
605
688
606
689
``` js
607
- const assert = require (' assert' );
690
+ const assert = require (' assert' ). strict ;
608
691
609
692
assert .strictEqual (1 , 2 );
610
693
// AssertionError: 1 === 2
@@ -728,8 +811,12 @@ For more information, see
728
811
[ `TypeError` ] : errors.html#errors_class_typeerror
729
812
[ `assert.deepEqual()` ] : #assert_assert_deepequal_actual_expected_message
730
813
[ `assert.deepStrictEqual()` ] : #assert_assert_deepstrictequal_actual_expected_message
814
+ [ `assert.notDeepStrictEqual()` ] : #assert_assert_notdeepstrictequal_actual_expected_message
815
+ [ `assert.notStrictEqual()` ] : #assert_assert_notstrictequal_actual_expected_message
731
816
[ `assert.ok()` ] : #assert_assert_ok_value_message
817
+ [ `assert.strictEqual()` ] : #assert_assert_strictequal_actual_expected_message
732
818
[ `assert.throws()` ] : #assert_assert_throws_block_error_message
819
+ [ `strict mode` ] : #assert_strict_mode
733
820
[ Abstract Equality Comparison ] : https://tc39.github.io/ecma262/#sec-abstract-equality-comparison
734
821
[ Object.prototype.toString() ] : https://tc39.github.io/ecma262/#sec-object.prototype.tostring
735
822
[ SameValueZero ] : https://tc39.github.io/ecma262/#sec-samevaluezero
0 commit comments