Skip to content

Commit

Permalink
assert: add checkPrototype option for assert.deepStrictEqual()
Browse files Browse the repository at this point in the history
Allow usage of assert.deepStrictEqual() without prototype checks.

Refs: nodejs#28011
  • Loading branch information
Trott committed Aug 27, 2019
1 parent 33ae95c commit ddef0ac
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 71 deletions.
30 changes: 19 additions & 11 deletions doc/api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,13 @@ parameter is undefined, a default error message is assigned. If the `message`
parameter is an instance of an [`Error`][] then it will be thrown instead of the
`AssertionError`.

## assert.deepStrictEqual(actual, expected[, message])
## assert.deepStrictEqual(actual, expected[, options])
<!-- YAML
added: v1.2.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/NNNNN
description: Added `checkPrototype` option.
- version: v9.0.0
pr-url: https://github.com/nodejs/node/pull/15169
description: Enumerable symbol properties are now compared.
Expand All @@ -291,11 +294,22 @@ changes:
-->
* `actual` {any}
* `expected` {any}
* `message` {string|Error}
* `options` {Object|string|Error}
* `checkPrototype` {boolean} Whether to check that `actual` and `expected`
have the same prototype. **Default:** `true`
* `error` {Error} Error to throw if `actual` and `expected` are not deep
equal. **Default:** `AssertionError`
* `message` {string} Message to use to replace the generated default message.
Ignored if `error` is specified.

Tests for deep equality between the `actual` and `expected` parameters.
"Deep" equality means that the enumerable "own" properties of child objects
are recursively evaluated also by the following rules.

If `options` is a string, it is the value of the `message` option.

If `options` is an `Error` object, then it is the value of the `error` option.

Enumerable "own" properties of child objects are recursively evaluated by the
following rules.

### Comparison details

Expand Down Expand Up @@ -408,12 +422,6 @@ assert.deepStrictEqual(weakMap1, weakMap3);
// }
```

If the values are not equal, an `AssertionError` is thrown with a `message`
property set equal to the value of the `message` parameter. If the `message`
parameter is undefined, a default error message is assigned. If the `message`
parameter is an instance of an [`Error`][] then it will be thrown instead of the
`AssertionError`.

## assert.doesNotReject(asyncFn[, error][, message])
<!-- YAML
added: v10.0.0
Expand Down Expand Up @@ -1278,7 +1286,7 @@ second argument. This might lead to difficult-to-spot errors.
[`WeakMap`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap
[`WeakSet`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet
[`assert.deepEqual()`]: #assert_assert_deepequal_actual_expected_message
[`assert.deepStrictEqual()`]: #assert_assert_deepstrictequal_actual_expected_message
[`assert.deepStrictEqual()`]: #assert_assert_deepstrictequal_actual_expected_options
[`assert.doesNotThrow()`]: #assert_assert_doesnotthrow_fn_error_message
[`assert.notDeepStrictEqual()`]: #assert_assert_notdeepstrictequal_actual_expected_message
[`assert.notStrictEqual()`]: #assert_assert_notstrictequal_actual_expected_message
Expand Down
2 changes: 1 addition & 1 deletion doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -2210,7 +2210,7 @@ util.log('Timestamped message.');
[`WeakMap`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap
[`WeakSet`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet
[`WebAssembly.Module`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module
[`assert.deepStrictEqual()`]: assert.html#assert_assert_deepstrictequal_actual_expected_message
[`assert.deepStrictEqual()`]: assert.html#assert_assert_deepstrictequal_actual_expected_options
[`console.error()`]: console.html#console_console_error_data_args
[`target` and `handler`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy#Terminology
[`tty.hasColors()`]: tty.html#tty_writestream_hascolors_count_env
Expand Down
38 changes: 23 additions & 15 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,21 +452,29 @@ assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
};
/* eslint-enable */

assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('actual', 'expected');
}
if (isDeepEqual === undefined) lazyLoadComparison();
if (!isDeepStrictEqual(actual, expected)) {
innerFail({
actual,
expected,
message,
operator: 'deepStrictEqual',
stackStartFn: deepStrictEqual
});
}
};
assert.deepStrictEqual =
function deepStrictEqual(actual, expected,
options = { checkPrototype: true }) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('actual', 'expected');
}
if (isDeepEqual === undefined) lazyLoadComparison();
if (!isDeepStrictEqual(actual, expected, options)) {
let message;
if (typeof options === 'string' || options instanceof Error) {
message = options;
} else {
message = options.error || options.message;
}
innerFail({
actual,
expected,
message,
operator: 'deepStrictEqual',
stackStartFn: deepStrictEqual
});
}
};

assert.notDeepStrictEqual = notDeepStrictEqual;
function notDeepStrictEqual(actual, expected, message) {
Expand Down
Loading

0 comments on commit ddef0ac

Please sign in to comment.