assert: add deep equal check for more Error type

PR-URL: https://github.com/nodejs/node/pull/51805
Fixes: https://github.com/nodejs/node/issues/51793
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
Zhenwei Jin authored and GitHub committed 2024-05-12 19:46:13 +00:00
1 parent c7d53f0169
commit c8a4f70265
3 files changed
+51 -7

No files matched your search

+11 -4
View File
@@ -555,6 +555,9 @@ An alias of [`assert.ok()`][].
<!-- YAML
added: v0.1.21
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/51805
description: Error cause and errors properties are now compared as well.
- version: v18.0.0
pr-url: https://github.com/nodejs/node/pull/41020
description: Regular expressions lastIndex property is now compared as well.
@@ -621,8 +624,8 @@ are also recursively evaluated by the following rules.
both sides are `NaN`.
* [Type tags][Object.prototype.toString()] of objects should be the same.
* Only [enumerable "own" properties][] are considered.
* [`Error`][] names and messages are always compared, even if these are not
enumerable properties.
* [`Error`][] names, messages, causes, and errors are always compared,
even if these are not enumerable properties.
* [Object wrappers][] are compared both as objects and unwrapped values.
* `Object` properties are compared unordered.
* [`Map`][] keys and [`Set`][] items are compared unordered.
@@ -736,6 +739,9 @@ parameter is an instance of an [`Error`][] then it will be thrown instead of the
<!-- YAML
added: v1.2.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/51805
description: Error cause and errors properties are now compared as well.
- version: v18.0.0
pr-url: https://github.com/nodejs/node/pull/41020
description: Regular expressions lastIndex property is now compared as well.
@@ -783,8 +789,9 @@ are recursively evaluated also by the following rules.
* [`[[Prototype]]`][prototype-spec] of objects are compared using
the [`===` operator][].
* Only [enumerable "own" properties][] are considered.
* [`Error`][] names and messages are always compared, even if these are not
enumerable properties.
* [`Error`][] names, messages, causes, and errors are always compared,
even if these are not enumerable properties.
`errors` is also compared.
* Enumerable own [`Symbol`][] properties are compared as well.
* [Object wrappers][] are compared both as objects and unwrapped values.
* `Object` properties are compared unordered.
+17 -3
View File
@@ -235,9 +235,23 @@ function innerDeepEqual(val1, val2, strict, memos) {
} else if (isNativeError(val1) || val1 instanceof Error) {
// Do not compare the stack as it might differ even though the error itself
// is otherwise identical.
if ((!isNativeError(val2) && !(val2 instanceof Error)) ||
val1.message !== val2.message ||
val1.name !== val2.name) {
if (!isNativeError(val2) && !(val2 instanceof Error)) {
return false;
}
const message1Enumerable = ObjectPrototypePropertyIsEnumerable(val1, 'message');
const name1Enumerable = ObjectPrototypePropertyIsEnumerable(val1, 'name');
const cause1Enumerable = ObjectPrototypePropertyIsEnumerable(val1, 'cause');
const errors1Enumerable = ObjectPrototypePropertyIsEnumerable(val1, 'errors');
if ((message1Enumerable !== ObjectPrototypePropertyIsEnumerable(val2, 'message') ||
(!message1Enumerable && val1.message !== val2.message)) ||
(name1Enumerable !== ObjectPrototypePropertyIsEnumerable(val2, 'name') ||
(!name1Enumerable && val1.name !== val2.name)) ||
(cause1Enumerable !== ObjectPrototypePropertyIsEnumerable(val2, 'cause') ||
(!cause1Enumerable && !innerDeepEqual(val1.cause, val2.cause, strict, memos))) ||
(errors1Enumerable !== ObjectPrototypePropertyIsEnumerable(val2, 'errors') ||
(!errors1Enumerable && !innerDeepEqual(val1.errors, val2.errors, strict, memos)))) {
return false;
}
} else if (isBoxedPrimitive(val1)) {
+23
View File
@@ -1111,6 +1111,29 @@ assert.throws(
assert.notDeepStrictEqual(err, err2);
}
// Check for Errors with cause property
{
const e1 = new Error('err', { cause: new Error('cause e1') });
const e2 = new Error('err', { cause: new Error('cause e2') });
assertNotDeepOrStrict(e1, e2, AssertionError);
assertNotDeepOrStrict(e1, new Error('err'), AssertionError);
assertDeepAndStrictEqual(e1, new Error('err', { cause: new Error('cause e1') }));
}
// Check for AggregateError
{
const e1 = new Error('e1');
const e1duplicate = new Error('e1');
const e2 = new Error('e2');
const e3 = new AggregateError([e1duplicate, e2], 'Aggregate Error');
const e3duplicate = new AggregateError([e1, e2], 'Aggregate Error');
const e4 = new AggregateError([e1], 'Aggregate Error');
assertNotDeepOrStrict(e1, e3, AssertionError);
assertNotDeepOrStrict(e3, e4, AssertionError);
assertDeepAndStrictEqual(e3, e3duplicate);
}
// Verify that `valueOf` is not called for boxed primitives.
{
const a = new Number(5);