77The ` assert ` module provides a simple set of assertion tests that can be used to
88test invariants.
99
10+ A ` strict ` and a ` legacy ` mode exist, while it is recommended to only use
11+ [ ` strict mode ` ] [ ] .
12+
1013For more information about the used equality comparisons see
1114[ MDN's guide on equality comparisons and sameness] [ mdn-equality-guide ] .
1215
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+
1361## assert(value[ , message] )
1462<!-- YAML
1563added: v0.5.9
@@ -43,6 +91,14 @@ changes:
4391* ` expected ` {any}
4492* ` message ` {any}
4593
94+ ** Strict mode**
95+
96+ An alias of [ ` assert.deepStrictEqual() ` ] [ ] .
97+
98+ ** Legacy mode**
99+
100+ > Stability: 0 - Deprecated: Use [ ` assert.deepStrictEqual() ` ] [ ] instead.
101+
46102Tests for deep equality between the ` actual ` and ` expected ` parameters.
47103Primitive values are compared with the [ Abstract Equality Comparison] [ ]
48104( ` == ` ).
@@ -147,7 +203,7 @@ are recursively evaluated also by the following rules.
147203 [ ` Object.is() ` ] [ ] .
148204* [ Type tags] [ Object.prototype.toString() ] of objects should be the same.
149205* [ ` [[Prototype]] ` ] [ prototype-spec ] of objects are compared using
150- the [ Strict Equality Comparison] [ ] too .
206+ the [ Strict Equality Comparison] [ ] .
151207* Only [ enumerable "own" properties] [ ] are considered.
152208* [ ` Error ` ] [ ] names and messages are always compared, even if these are not
153209 enumerable properties.
@@ -159,7 +215,7 @@ are recursively evaluated also by the following rules.
159215 reference.
160216
161217``` js
162- const assert = require (' assert' );
218+ const assert = require (' assert' ). strict ;
163219
164220assert .deepStrictEqual ({ a: 1 }, { a: ' 1' });
165221// AssertionError: { a: 1 } deepStrictEqual { a: '1' }
@@ -279,6 +335,14 @@ added: v0.1.21
279335* ` expected ` {any}
280336* ` message ` {any}
281337
338+ ** Strict mode**
339+
340+ An alias of [ ` assert.strictEqual() ` ] [ ] .
341+
342+ ** Legacy mode**
343+
344+ > Stability: 0 - Deprecated: Use [ ` assert.strictEqual() ` ] [ ] instead.
345+
282346Tests shallow, coercive equality between the ` actual ` and ` expected ` parameters
283347using the [ Abstract Equality Comparison] [ ] ( ` == ` ).
284348
@@ -325,7 +389,7 @@ all stack frames above that function will be removed from stacktrace (see
325389` Failed ` will be used.
326390
327391``` js
328- const assert = require (' assert' );
392+ const assert = require (' assert' ). strict ;
329393
330394assert .fail (1 , 2 , undefined , ' >' );
331395// AssertionError [ERR_ASSERTION]: 1 > 2
@@ -376,7 +440,7 @@ Throws `value` if `value` is truthy. This is useful when testing the `error`
376440argument in callbacks.
377441
378442``` js
379- const assert = require (' assert' );
443+ const assert = require (' assert' ). strict ;
380444
381445assert .ifError (0 );
382446// OK
@@ -412,6 +476,14 @@ changes:
412476* ` expected ` {any}
413477* ` message ` {any}
414478
479+ ** Strict mode**
480+
481+ An alias of [ ` assert.notDeepStrictEqual() ` ] [ ] .
482+
483+ ** Legacy mode**
484+
485+ > Stability: 0 - Deprecated: Use [ ` assert.notDeepStrictEqual() ` ] [ ] instead.
486+
415487Tests for any deep inequality. Opposite of [ ` assert.deepEqual() ` ] [ ] .
416488
417489``` js
@@ -486,7 +558,7 @@ changes:
486558Tests for deep strict inequality. Opposite of [ ` assert.deepStrictEqual() ` ] [ ] .
487559
488560``` js
489- const assert = require (' assert' );
561+ const assert = require (' assert' ). strict ;
490562
491563assert .notDeepStrictEqual ({ a: 1 }, { a: ' 1' });
492564// OK
@@ -506,6 +578,14 @@ added: v0.1.21
506578* ` expected ` {any}
507579* ` message ` {any}
508580
581+ ** Strict mode**
582+
583+ An alias of [ ` assert.notStrictEqual() ` ] [ ] .
584+
585+ ** Legacy mode**
586+
587+ > Stability: 0 - Deprecated: Use [ ` assert.notStrictEqual() ` ] [ ] instead.
588+
509589Tests shallow, coercive inequality with the [ Abstract Equality Comparison] [ ]
510590( ` != ` ).
511591
@@ -544,7 +624,7 @@ Tests strict inequality between the `actual` and `expected` parameters as
544624determined by the [ SameValue Comparison] [ ] .
545625
546626``` js
547- const assert = require (' assert' );
627+ const assert = require (' assert' ). strict ;
548628
549629assert .notStrictEqual (1 , 2 );
550630// OK
@@ -579,7 +659,7 @@ parameter is an instance of an [`Error`][] then it will be thrown instead of the
579659` AssertionError ` .
580660
581661``` js
582- const assert = require (' assert' );
662+ const assert = require (' assert' ). strict ;
583663
584664assert .ok (true );
585665// OK
@@ -609,7 +689,7 @@ Tests strict equality between the `actual` and `expected` parameters as
609689determined by the [ SameValue Comparison] [ ] .
610690
611691``` js
612- const assert = require (' assert' );
692+ const assert = require (' assert' ). strict ;
613693
614694assert .strictEqual (1 , 2 );
615695// AssertionError: 1 strictEqual 2
@@ -707,8 +787,12 @@ assert.throws(myFunction, /missing foo/, 'did not throw with expected message');
707787[ `TypeError` ] : errors.html#errors_class_typeerror
708788[ `assert.deepEqual()` ] : #assert_assert_deepequal_actual_expected_message
709789[ `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
710792[ `assert.ok()` ] : #assert_assert_ok_value_message
793+ [ `assert.strictEqual()` ] : #assert_assert_strictequal_actual_expected_message
711794[ `assert.throws()` ] : #assert_assert_throws_block_error_message
795+ [ `strict mode` ] : #assert_strict_mode
712796[ Abstract Equality Comparison ] : https://tc39.github.io/ecma262/#sec-abstract-equality-comparison
713797[ Object.prototype.toString() ] : https://tc39.github.io/ecma262/#sec-object.prototype.tostring
714798[ SameValue Comparison ] : https://tc39.github.io/ecma262/#sec-samevalue
0 commit comments