Skip to content

Commit a319e90

Browse files
BridgeARaddaleax
authored andcommitted
assert: add strict functionality export
Requireing the strict version will allow to use `assert.equal`, `assert.deepEqual` and there negated counterparts to be used with strict comparison instead of using e.g. `assert.strictEqual`. The API is identical to the regular assert export and only differs in the way that all functions use strict compairson. PR-URL: #17002 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 06ab6f2 commit a319e90

File tree

4 files changed

+132
-8
lines changed

4 files changed

+132
-8
lines changed

doc/api/assert.md

+92-8
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,57 @@
77
The `assert` module provides a simple set of assertion tests that can be used to
88
test invariants.
99

10+
A `strict` and a `legacy` mode exist, while it is recommended to only use
11+
[`strict mode`][].
12+
1013
For 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
1563
added: 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+
46102
Tests for deep equality between the `actual` and `expected` parameters.
47103
Primitive 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

164220
assert.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+
282346
Tests shallow, coercive equality between the `actual` and `expected` parameters
283347
using 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

330394
assert.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`
376440
argument in callbacks.
377441

378442
```js
379-
const assert = require('assert');
443+
const assert = require('assert').strict;
380444

381445
assert.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+
415487
Tests for any deep inequality. Opposite of [`assert.deepEqual()`][].
416488

417489
```js
@@ -486,7 +558,7 @@ changes:
486558
Tests for deep strict inequality. Opposite of [`assert.deepStrictEqual()`][].
487559

488560
```js
489-
const assert = require('assert');
561+
const assert = require('assert').strict;
490562

491563
assert.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+
509589
Tests shallow, coercive inequality with the [Abstract Equality Comparison][]
510590
( `!=` ).
511591

@@ -544,7 +624,7 @@ Tests strict inequality between the `actual` and `expected` parameters as
544624
determined by the [SameValue Comparison][].
545625

546626
```js
547-
const assert = require('assert');
627+
const assert = require('assert').strict;
548628

549629
assert.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

584664
assert.ok(true);
585665
// OK
@@ -609,7 +689,7 @@ Tests strict equality between the `actual` and `expected` parameters as
609689
determined by the [SameValue Comparison][].
610690

611691
```js
612-
const assert = require('assert');
692+
const assert = require('assert').strict;
613693

614694
assert.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

doc/api/deprecations.md

+9
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,15 @@ Type: End-of-Life
798798
cause a lot of issues. See https://github.com/nodejs/node/issues/14328 for more
799799
details.
800800
801+
<a id="DEP0089"></a>
802+
### DEP0089: require('assert')
803+
804+
Type: Documentation-only
805+
806+
Importing assert directly is not recommended as the exposed functions will use
807+
loose equality checks. Use `require('assert').strict` instead. The API is the
808+
same as the legacy assert but it will always use strict equality checks.
809+
801810
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
802811
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array
803812
[`Buffer.from(buffer)`]: buffer.html#buffer_class_method_buffer_from_buffer

lib/assert.js

+12
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,15 @@ assert.doesNotThrow = function doesNotThrow(block, error, message) {
207207
};
208208

209209
assert.ifError = function ifError(err) { if (err) throw err; };
210+
211+
// Expose a strict only variant of assert
212+
function strict(value, message) {
213+
if (!value) innerFail(value, true, message, '==', strict);
214+
}
215+
assert.strict = Object.assign(strict, assert, {
216+
equal: assert.strictEqual,
217+
deepEqual: assert.deepStrictEqual,
218+
notEqual: assert.notStrictEqual,
219+
notDeepEqual: assert.notDeepStrictEqual
220+
});
221+
assert.strict.strict = assert.strict;

test/parallel/test-assert.js

+19
Original file line numberDiff line numberDiff line change
@@ -752,3 +752,22 @@ common.expectsError(
752752
message: /^'Error: foo' strictEqual 'Error: foobar'$/
753753
}
754754
);
755+
756+
// Test strict assert
757+
{
758+
const a = require('assert');
759+
const assert = require('assert').strict;
760+
/* eslint-disable no-restricted-properties */
761+
assert.throws(() => assert.equal(1, true), assert.AssertionError);
762+
assert.notEqual(0, false);
763+
assert.throws(() => assert.deepEqual(1, true), assert.AssertionError);
764+
assert.notDeepEqual(0, false);
765+
assert.equal(assert.strict, assert.strict.strict);
766+
assert.equal(assert.equal, assert.strictEqual);
767+
assert.equal(assert.deepEqual, assert.deepStrictEqual);
768+
assert.equal(assert.notEqual, assert.notStrictEqual);
769+
assert.equal(assert.notDeepEqual, assert.notDeepStrictEqual);
770+
assert.equal(Object.keys(assert).length, Object.keys(a).length);
771+
/* eslint-enable no-restricted-properties */
772+
assert(7);
773+
}

0 commit comments

Comments
 (0)