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
+
10
13
For more information about the used equality comparisons see
11
14
[ MDN's guide on equality comparisons and sameness] [ mdn-equality-guide ] .
12
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
+
13
61
## assert(value[ , message] )
14
62
<!-- YAML
15
63
added: v0.5.9
@@ -43,6 +91,14 @@ changes:
43
91
* ` expected ` {any}
44
92
* ` message ` {any}
45
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
+
46
102
Tests for deep equality between the ` actual ` and ` expected ` parameters.
47
103
Primitive values are compared with the [ Abstract Equality Comparison] [ ]
48
104
( ` == ` ).
@@ -147,7 +203,7 @@ are recursively evaluated also by the following rules.
147
203
[ ` Object.is() ` ] [ ] .
148
204
* [ Type tags] [ Object.prototype.toString() ] of objects should be the same.
149
205
* [ ` [[Prototype]] ` ] [ prototype-spec ] of objects are compared using
150
- the [ Strict Equality Comparison] [ ] too .
206
+ the [ Strict Equality Comparison] [ ] .
151
207
* Only [ enumerable "own" properties] [ ] are considered.
152
208
* [ ` Error ` ] [ ] names and messages are always compared, even if these are not
153
209
enumerable properties.
@@ -159,7 +215,7 @@ are recursively evaluated also by the following rules.
159
215
reference.
160
216
161
217
``` js
162
- const assert = require (' assert' );
218
+ const assert = require (' assert' ). strict ;
163
219
164
220
assert .deepStrictEqual ({ a: 1 }, { a: ' 1' });
165
221
// AssertionError: { a: 1 } deepStrictEqual { a: '1' }
@@ -279,6 +335,14 @@ added: v0.1.21
279
335
* ` expected ` {any}
280
336
* ` message ` {any}
281
337
338
+ ** Strict mode**
339
+
340
+ An alias of [ ` assert.strictEqual() ` ] [ ] .
341
+
342
+ ** Legacy mode**
343
+
344
+ > Stability: 0 - Deprecated: Use [ ` assert.strictEqual() ` ] [ ] instead.
345
+
282
346
Tests shallow, coercive equality between the ` actual ` and ` expected ` parameters
283
347
using the [ Abstract Equality Comparison] [ ] ( ` == ` ).
284
348
@@ -325,7 +389,7 @@ all stack frames above that function will be removed from stacktrace (see
325
389
` Failed ` will be used.
326
390
327
391
``` js
328
- const assert = require (' assert' );
392
+ const assert = require (' assert' ). strict ;
329
393
330
394
assert .fail (1 , 2 , undefined , ' >' );
331
395
// AssertionError [ERR_ASSERTION]: 1 > 2
@@ -376,7 +440,7 @@ Throws `value` if `value` is truthy. This is useful when testing the `error`
376
440
argument in callbacks.
377
441
378
442
``` js
379
- const assert = require (' assert' );
443
+ const assert = require (' assert' ). strict ;
380
444
381
445
assert .ifError (0 );
382
446
// OK
@@ -412,6 +476,14 @@ changes:
412
476
* ` expected ` {any}
413
477
* ` message ` {any}
414
478
479
+ ** Strict mode**
480
+
481
+ An alias of [ ` assert.notDeepStrictEqual() ` ] [ ] .
482
+
483
+ ** Legacy mode**
484
+
485
+ > Stability: 0 - Deprecated: Use [ ` assert.notDeepStrictEqual() ` ] [ ] instead.
486
+
415
487
Tests for any deep inequality. Opposite of [ ` assert.deepEqual() ` ] [ ] .
416
488
417
489
``` js
@@ -486,7 +558,7 @@ changes:
486
558
Tests for deep strict inequality. Opposite of [ ` assert.deepStrictEqual() ` ] [ ] .
487
559
488
560
``` js
489
- const assert = require (' assert' );
561
+ const assert = require (' assert' ). strict ;
490
562
491
563
assert .notDeepStrictEqual ({ a: 1 }, { a: ' 1' });
492
564
// OK
@@ -506,6 +578,14 @@ added: v0.1.21
506
578
* ` expected ` {any}
507
579
* ` message ` {any}
508
580
581
+ ** Strict mode**
582
+
583
+ An alias of [ ` assert.notStrictEqual() ` ] [ ] .
584
+
585
+ ** Legacy mode**
586
+
587
+ > Stability: 0 - Deprecated: Use [ ` assert.notStrictEqual() ` ] [ ] instead.
588
+
509
589
Tests shallow, coercive inequality with the [ Abstract Equality Comparison] [ ]
510
590
( ` != ` ).
511
591
@@ -544,7 +624,7 @@ Tests strict inequality between the `actual` and `expected` parameters as
544
624
determined by the [ SameValue Comparison] [ ] .
545
625
546
626
``` js
547
- const assert = require (' assert' );
627
+ const assert = require (' assert' ). strict ;
548
628
549
629
assert .notStrictEqual (1 , 2 );
550
630
// OK
@@ -579,7 +659,7 @@ parameter is an instance of an [`Error`][] then it will be thrown instead of the
579
659
` AssertionError ` .
580
660
581
661
``` js
582
- const assert = require (' assert' );
662
+ const assert = require (' assert' ). strict ;
583
663
584
664
assert .ok (true );
585
665
// OK
@@ -609,7 +689,7 @@ Tests strict equality between the `actual` and `expected` parameters as
609
689
determined by the [ SameValue Comparison] [ ] .
610
690
611
691
``` js
612
- const assert = require (' assert' );
692
+ const assert = require (' assert' ). strict ;
613
693
614
694
assert .strictEqual (1 , 2 );
615
695
// AssertionError: 1 strictEqual 2
@@ -707,8 +787,12 @@ assert.throws(myFunction, /missing foo/, 'did not throw with expected message');
707
787
[ `TypeError` ] : errors.html#errors_class_typeerror
708
788
[ `assert.deepEqual()` ] : #assert_assert_deepequal_actual_expected_message
709
789
[ `assert.deepStrictEqual()` ] : #assert_assert_deepstrictequal_actual_expected_message
790
+ [ `assert.notDeepStrictEqual()` ] : #assert_assert_notdeepstrictequal_actual_expected_message
791
+ [ `assert.notStrictEqual()` ] : #assert_assert_notstrictequal_actual_expected_message
710
792
[ `assert.ok()` ] : #assert_assert_ok_value_message
793
+ [ `assert.strictEqual()` ] : #assert_assert_strictequal_actual_expected_message
711
794
[ `assert.throws()` ] : #assert_assert_throws_block_error_message
795
+ [ `strict mode` ] : #assert_strict_mode
712
796
[ Abstract Equality Comparison ] : https://tc39.github.io/ecma262/#sec-abstract-equality-comparison
713
797
[ Object.prototype.toString() ] : https://tc39.github.io/ecma262/#sec-object.prototype.tostring
714
798
[ SameValue Comparison ] : https://tc39.github.io/ecma262/#sec-samevalue
0 commit comments