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
@@ -37,6 +88,14 @@ changes:
37
88
* ` expected ` {any}
38
89
* ` message ` {any}
39
90
91
+ ** Strict mode**
92
+
93
+ An alias of [ ` assert.deepStrictEqual() ` ] [ ] .
94
+
95
+ ** Legacy mode**
96
+
97
+ > Stability: 0 - Deprecated: Use [ ` assert.deepStrictEqual() ` ] [ ] instead.
98
+
40
99
Tests for deep equality between the ` actual ` and ` expected ` parameters.
41
100
Primitive values are compared with the [ Abstract Equality Comparison] [ ]
42
101
( ` == ` ).
@@ -126,16 +185,26 @@ changes:
126
185
127
186
Generally identical to ` assert.deepEqual() ` with a few exceptions:
128
187
129
- 1 . Primitive values are compared using the [ Strict Equality Comparison] [ ]
130
- ( ` === ` ). Set values and Map keys are compared using the [ SameValueZero] [ ]
188
+ ### Comparison details
189
+
190
+ * Primitive values are compared using the [ Strict Equality Comparison] [ ]
191
+ ( ` === ` ).
192
+ * Set values and Map keys are compared using the [ SameValueZero] [ ]
131
193
comparison. (Which means they are free of the [ caveats] [ ] ).
132
- 2 . [ ` [[Prototype]] ` ] [ prototype-spec ] of objects are compared using
133
- the [ Strict Equality Comparison] [ ] too.
134
- 3 . [ Type tags] [ Object.prototype.toString() ] of objects should be the same.
135
- 4 . [ Object wrappers] [ ] are compared both as objects and unwrapped values.
194
+ * [ Type tags] [ Object.prototype.toString() ] of objects should be the same.
195
+ * [ ` [[Prototype]] ` ] [ prototype-spec ] of objects are compared using
196
+ the [ Strict Equality Comparison] [ ] .
197
+ * Only [ enumerable "own" properties] [ ] are considered.
198
+ * [ ` Error ` ] [ ] messages are always compared, even though this property is
199
+ non-enumerable.
200
+ * [ Object wrappers] [ ] are compared both as objects and unwrapped values.
201
+ * Object properties are compared unordered.
202
+ * Map keys and Set items are compared unordered.
203
+ * Recursion stops when both sides differ or both sides encounter a circular
204
+ reference.
136
205
137
206
``` js
138
- const assert = require (' assert' );
207
+ const assert = require (' assert' ). strict ;
139
208
140
209
assert .deepEqual ({ a: 1 }, { a: ' 1' });
141
210
// OK, because 1 == '1'
@@ -251,6 +320,14 @@ added: v0.1.21
251
320
* ` expected ` {any}
252
321
* ` message ` {any}
253
322
323
+ ** Strict mode**
324
+
325
+ An alias of [ ` assert.strictEqual() ` ] [ ] .
326
+
327
+ ** Legacy mode**
328
+
329
+ > Stability: 0 - Deprecated: Use [ ` assert.strictEqual() ` ] [ ] instead.
330
+
254
331
Tests shallow, coercive equality between the ` actual ` and ` expected ` parameters
255
332
using the [ Abstract Equality Comparison] [ ] ( ` == ` ).
256
333
@@ -292,7 +369,7 @@ If `stackStartFunction` is provided, all stack frames above that function will
292
369
be removed from stacktrace (see [ ` Error.captureStackTrace ` ] ).
293
370
294
371
``` js
295
- const assert = require (' assert' );
372
+ const assert = require (' assert' ). strict ;
296
373
297
374
assert .fail (1 , 2 , undefined , ' >' );
298
375
// AssertionError [ERR_ASSERTION]: 1 > 2
@@ -340,7 +417,7 @@ Throws `value` if `value` is truthy. This is useful when testing the `error`
340
417
argument in callbacks.
341
418
342
419
``` js
343
- const assert = require (' assert' );
420
+ const assert = require (' assert' ). strict ;
344
421
345
422
assert .ifError (null );
346
423
// OK
@@ -362,6 +439,14 @@ added: v0.1.21
362
439
* ` expected ` {any}
363
440
* ` message ` {any}
364
441
442
+ ** Strict mode**
443
+
444
+ An alias of [ ` assert.notDeepStrictEqual() ` ] [ ] .
445
+
446
+ ** Legacy mode**
447
+
448
+ > Stability: 0 - Deprecated: Use [ ` assert.notDeepStrictEqual() ` ] [ ] instead.
449
+
365
450
Tests for any deep inequality. Opposite of [ ` assert.deepEqual() ` ] [ ] .
366
451
367
452
``` js
@@ -412,7 +497,7 @@ added: v1.2.0
412
497
Tests for deep strict inequality. Opposite of [ ` assert.deepStrictEqual() ` ] [ ] .
413
498
414
499
``` js
415
- const assert = require (' assert' );
500
+ const assert = require (' assert' ). strict ;
416
501
417
502
assert .notDeepEqual ({ a: 1 }, { a: ' 1' });
418
503
// AssertionError: { a: 1 } notDeepEqual { a: '1' }
@@ -433,6 +518,14 @@ added: v0.1.21
433
518
* ` expected ` {any}
434
519
* ` message ` {any}
435
520
521
+ ** Strict mode**
522
+
523
+ An alias of [ ` assert.notStrictEqual() ` ] [ ] .
524
+
525
+ ** Legacy mode**
526
+
527
+ > Stability: 0 - Deprecated: Use [ ` assert.notStrictEqual() ` ] [ ] instead.
528
+
436
529
Tests shallow, coercive inequality with the [ Abstract Equality Comparison] [ ]
437
530
( ` != ` ).
438
531
@@ -465,7 +558,7 @@ Tests strict inequality as determined by the [Strict Equality Comparison][]
465
558
( ` !== ` ).
466
559
467
560
``` js
468
- const assert = require (' assert' );
561
+ const assert = require (' assert' ). strict ;
469
562
470
563
assert .notStrictEqual (1 , 2 );
471
564
// OK
@@ -496,7 +589,7 @@ property set equal to the value of the `message` parameter. If the `message`
496
589
parameter is ` undefined ` , a default error message is assigned.
497
590
498
591
``` js
499
- const assert = require (' assert' );
592
+ const assert = require (' assert' ). strict ;
500
593
501
594
assert .ok (true );
502
595
// OK
@@ -522,7 +615,7 @@ Tests strict equality as determined by the [Strict Equality Comparison][]
522
615
( ` === ` ).
523
616
524
617
``` js
525
- const assert = require (' assert' );
618
+ const assert = require (' assert' ). strict ;
526
619
527
620
assert .strictEqual (1 , 2 );
528
621
// AssertionError: 1 === 2
@@ -643,8 +736,12 @@ For more information, see
643
736
[ `TypeError` ] : errors.html#errors_class_typeerror
644
737
[ `assert.deepEqual()` ] : #assert_assert_deepequal_actual_expected_message
645
738
[ `assert.deepStrictEqual()` ] : #assert_assert_deepstrictequal_actual_expected_message
739
+ [ `assert.notDeepStrictEqual()` ] : #assert_assert_notdeepstrictequal_actual_expected_message
740
+ [ `assert.notStrictEqual()` ] : #assert_assert_notstrictequal_actual_expected_message
646
741
[ `assert.ok()` ] : #assert_assert_ok_value_message
742
+ [ `assert.strictEqual()` ] : #assert_assert_strictequal_actual_expected_message
647
743
[ `assert.throws()` ] : #assert_assert_throws_block_error_message
744
+ [ `strict mode` ] : #assert_strict_mode
648
745
[ Abstract Equality Comparison ] : https://tc39.github.io/ecma262/#sec-abstract-equality-comparison
649
746
[ Object.prototype.toString() ] : https://tc39.github.io/ecma262/#sec-object.prototype.tostring
650
747
[ SameValueZero ] : https://tc39.github.io/ecma262/#sec-samevaluezero
0 commit comments