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: https://github.com/nodejs/node/pull/17002
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Ruben Bridgewater 2017-11-13 18:15:24 -02:00 committed by Anna Henningsen
parent 06ab6f2f13
commit a319e90807
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
4 changed files with 132 additions and 8 deletions

View File

@ -7,9 +7,57 @@
The `assert` module provides a simple set of assertion tests that can be used to The `assert` module provides a simple set of assertion tests that can be used to
test invariants. test invariants.
A `strict` and a `legacy` mode exist, while it is recommended to only use
[`strict mode`][].
For more information about the used equality comparisons see For more information about the used equality comparisons see
[MDN's guide on equality comparisons and sameness][mdn-equality-guide]. [MDN's guide on equality comparisons and sameness][mdn-equality-guide].
## Strict mode
<!-- YAML
added: REPLACEME
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/17002
description: Added strict mode to the assert module.
-->
When using the `strict mode`, any `assert` function will use the equality used in
the strict function mode. So [`assert.deepEqual()`][] will, for example, work the
same as [`assert.deepStrictEqual()`][].
It can be accessed using:
```js
const assert = require('assert').strict;
```
## Legacy mode
> Stability: 0 - Deprecated: Use strict mode instead.
When accessing `assert` directly instead of using the `strict` property, the
[Abstract Equality Comparison][] will be used for any function without a
"strict" in its name (e.g. [`assert.deepEqual()`][]).
It can be accessed using:
```js
const assert = require('assert');
```
It is recommended to use the [`strict mode`][] instead as the
[Abstract Equality Comparison][] can often have surprising results. Especially
in case of [`assert.deepEqual()`][] as the used comparison rules there are very
lax.
E.g.
```js
// WARNING: This does not throw an AssertionError!
assert.deepEqual(/a/gi, new Date());
```
## assert(value[, message]) ## assert(value[, message])
<!-- YAML <!-- YAML
added: v0.5.9 added: v0.5.9
@ -43,6 +91,14 @@ changes:
* `expected` {any} * `expected` {any}
* `message` {any} * `message` {any}
**Strict mode**
An alias of [`assert.deepStrictEqual()`][].
**Legacy mode**
> Stability: 0 - Deprecated: Use [`assert.deepStrictEqual()`][] instead.
Tests for deep equality between the `actual` and `expected` parameters. Tests for deep equality between the `actual` and `expected` parameters.
Primitive values are compared with the [Abstract Equality Comparison][] Primitive values are compared with the [Abstract Equality Comparison][]
( `==` ). ( `==` ).
@ -147,7 +203,7 @@ are recursively evaluated also by the following rules.
[`Object.is()`][]. [`Object.is()`][].
* [Type tags][Object.prototype.toString()] of objects should be the same. * [Type tags][Object.prototype.toString()] of objects should be the same.
* [`[[Prototype]]`][prototype-spec] of objects are compared using * [`[[Prototype]]`][prototype-spec] of objects are compared using
the [Strict Equality Comparison][] too. the [Strict Equality Comparison][].
* Only [enumerable "own" properties][] are considered. * Only [enumerable "own" properties][] are considered.
* [`Error`][] names and messages are always compared, even if these are not * [`Error`][] names and messages are always compared, even if these are not
enumerable properties. enumerable properties.
@ -159,7 +215,7 @@ are recursively evaluated also by the following rules.
reference. reference.
```js ```js
const assert = require('assert'); const assert = require('assert').strict;
assert.deepStrictEqual({ a: 1 }, { a: '1' }); assert.deepStrictEqual({ a: 1 }, { a: '1' });
// AssertionError: { a: 1 } deepStrictEqual { a: '1' } // AssertionError: { a: 1 } deepStrictEqual { a: '1' }
@ -279,6 +335,14 @@ added: v0.1.21
* `expected` {any} * `expected` {any}
* `message` {any} * `message` {any}
**Strict mode**
An alias of [`assert.strictEqual()`][].
**Legacy mode**
> Stability: 0 - Deprecated: Use [`assert.strictEqual()`][] instead.
Tests shallow, coercive equality between the `actual` and `expected` parameters Tests shallow, coercive equality between the `actual` and `expected` parameters
using the [Abstract Equality Comparison][] ( `==` ). using the [Abstract Equality Comparison][] ( `==` ).
@ -325,7 +389,7 @@ all stack frames above that function will be removed from stacktrace (see
`Failed` will be used. `Failed` will be used.
```js ```js
const assert = require('assert'); const assert = require('assert').strict;
assert.fail(1, 2, undefined, '>'); assert.fail(1, 2, undefined, '>');
// AssertionError [ERR_ASSERTION]: 1 > 2 // AssertionError [ERR_ASSERTION]: 1 > 2
@ -376,7 +440,7 @@ Throws `value` if `value` is truthy. This is useful when testing the `error`
argument in callbacks. argument in callbacks.
```js ```js
const assert = require('assert'); const assert = require('assert').strict;
assert.ifError(0); assert.ifError(0);
// OK // OK
@ -412,6 +476,14 @@ changes:
* `expected` {any} * `expected` {any}
* `message` {any} * `message` {any}
**Strict mode**
An alias of [`assert.notDeepStrictEqual()`][].
**Legacy mode**
> Stability: 0 - Deprecated: Use [`assert.notDeepStrictEqual()`][] instead.
Tests for any deep inequality. Opposite of [`assert.deepEqual()`][]. Tests for any deep inequality. Opposite of [`assert.deepEqual()`][].
```js ```js
@ -486,7 +558,7 @@ changes:
Tests for deep strict inequality. Opposite of [`assert.deepStrictEqual()`][]. Tests for deep strict inequality. Opposite of [`assert.deepStrictEqual()`][].
```js ```js
const assert = require('assert'); const assert = require('assert').strict;
assert.notDeepStrictEqual({ a: 1 }, { a: '1' }); assert.notDeepStrictEqual({ a: 1 }, { a: '1' });
// OK // OK
@ -506,6 +578,14 @@ added: v0.1.21
* `expected` {any} * `expected` {any}
* `message` {any} * `message` {any}
**Strict mode**
An alias of [`assert.notStrictEqual()`][].
**Legacy mode**
> Stability: 0 - Deprecated: Use [`assert.notStrictEqual()`][] instead.
Tests shallow, coercive inequality with the [Abstract Equality Comparison][] Tests shallow, coercive inequality with the [Abstract Equality Comparison][]
( `!=` ). ( `!=` ).
@ -544,7 +624,7 @@ Tests strict inequality between the `actual` and `expected` parameters as
determined by the [SameValue Comparison][]. determined by the [SameValue Comparison][].
```js ```js
const assert = require('assert'); const assert = require('assert').strict;
assert.notStrictEqual(1, 2); assert.notStrictEqual(1, 2);
// OK // OK
@ -579,7 +659,7 @@ parameter is an instance of an [`Error`][] then it will be thrown instead of the
`AssertionError`. `AssertionError`.
```js ```js
const assert = require('assert'); const assert = require('assert').strict;
assert.ok(true); assert.ok(true);
// OK // OK
@ -609,7 +689,7 @@ Tests strict equality between the `actual` and `expected` parameters as
determined by the [SameValue Comparison][]. determined by the [SameValue Comparison][].
```js ```js
const assert = require('assert'); const assert = require('assert').strict;
assert.strictEqual(1, 2); assert.strictEqual(1, 2);
// AssertionError: 1 strictEqual 2 // AssertionError: 1 strictEqual 2
@ -707,8 +787,12 @@ assert.throws(myFunction, /missing foo/, 'did not throw with expected message');
[`TypeError`]: errors.html#errors_class_typeerror [`TypeError`]: errors.html#errors_class_typeerror
[`assert.deepEqual()`]: #assert_assert_deepequal_actual_expected_message [`assert.deepEqual()`]: #assert_assert_deepequal_actual_expected_message
[`assert.deepStrictEqual()`]: #assert_assert_deepstrictequal_actual_expected_message [`assert.deepStrictEqual()`]: #assert_assert_deepstrictequal_actual_expected_message
[`assert.notDeepStrictEqual()`]: #assert_assert_notdeepstrictequal_actual_expected_message
[`assert.notStrictEqual()`]: #assert_assert_notstrictequal_actual_expected_message
[`assert.ok()`]: #assert_assert_ok_value_message [`assert.ok()`]: #assert_assert_ok_value_message
[`assert.strictEqual()`]: #assert_assert_strictequal_actual_expected_message
[`assert.throws()`]: #assert_assert_throws_block_error_message [`assert.throws()`]: #assert_assert_throws_block_error_message
[`strict mode`]: #assert_strict_mode
[Abstract Equality Comparison]: https://tc39.github.io/ecma262/#sec-abstract-equality-comparison [Abstract Equality Comparison]: https://tc39.github.io/ecma262/#sec-abstract-equality-comparison
[Object.prototype.toString()]: https://tc39.github.io/ecma262/#sec-object.prototype.tostring [Object.prototype.toString()]: https://tc39.github.io/ecma262/#sec-object.prototype.tostring
[SameValue Comparison]: https://tc39.github.io/ecma262/#sec-samevalue [SameValue Comparison]: https://tc39.github.io/ecma262/#sec-samevalue

View File

@ -798,6 +798,15 @@ Type: End-of-Life
cause a lot of issues. See https://github.com/nodejs/node/issues/14328 for more cause a lot of issues. See https://github.com/nodejs/node/issues/14328 for more
details. details.
<a id="DEP0089"></a>
### DEP0089: require('assert')
Type: Documentation-only
Importing assert directly is not recommended as the exposed functions will use
loose equality checks. Use `require('assert').strict` instead. The API is the
same as the legacy assert but it will always use strict equality checks.
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size [`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array [`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array
[`Buffer.from(buffer)`]: buffer.html#buffer_class_method_buffer_from_buffer [`Buffer.from(buffer)`]: buffer.html#buffer_class_method_buffer_from_buffer

View File

@ -207,3 +207,15 @@ assert.doesNotThrow = function doesNotThrow(block, error, message) {
}; };
assert.ifError = function ifError(err) { if (err) throw err; }; assert.ifError = function ifError(err) { if (err) throw err; };
// Expose a strict only variant of assert
function strict(value, message) {
if (!value) innerFail(value, true, message, '==', strict);
}
assert.strict = Object.assign(strict, assert, {
equal: assert.strictEqual,
deepEqual: assert.deepStrictEqual,
notEqual: assert.notStrictEqual,
notDeepEqual: assert.notDeepStrictEqual
});
assert.strict.strict = assert.strict;

View File

@ -752,3 +752,22 @@ common.expectsError(
message: /^'Error: foo' strictEqual 'Error: foobar'$/ message: /^'Error: foo' strictEqual 'Error: foobar'$/
} }
); );
// Test strict assert
{
const a = require('assert');
const assert = require('assert').strict;
/* eslint-disable no-restricted-properties */
assert.throws(() => assert.equal(1, true), assert.AssertionError);
assert.notEqual(0, false);
assert.throws(() => assert.deepEqual(1, true), assert.AssertionError);
assert.notDeepEqual(0, false);
assert.equal(assert.strict, assert.strict.strict);
assert.equal(assert.equal, assert.strictEqual);
assert.equal(assert.deepEqual, assert.deepStrictEqual);
assert.equal(assert.notEqual, assert.notStrictEqual);
assert.equal(assert.notDeepEqual, assert.notDeepStrictEqual);
assert.equal(Object.keys(assert).length, Object.keys(a).length);
/* eslint-enable no-restricted-properties */
assert(7);
}