Skip to content

Commit bd5f30d

Browse files
committed
assert: add deep equal check for more Error type
1 parent 0550bc1 commit bd5f30d

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

doc/api/assert.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,9 @@ are also recursively evaluated by the following rules.
621621
both sides are `NaN`.
622622
* [Type tags][Object.prototype.toString()] of objects should be the same.
623623
* Only [enumerable "own" properties][] are considered.
624-
* [`Error`][] names and messages are always compared, even if these are not
625-
enumerable properties.
624+
* [`Error`][] names, messages and causes are always compared, even if these are
625+
not enumerable properties. For `AggregateError`, non-enumerable property
626+
`errors` is also compared.
626627
* [Object wrappers][] are compared both as objects and unwrapped values.
627628
* `Object` properties are compared unordered.
628629
* [`Map`][] keys and [`Set`][] items are compared unordered.

lib/internal/util/comparisons.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const {
2323
TypedArrayPrototypeGetSymbolToStringTag,
2424
TypedArrayPrototypeGetByteLength,
2525
Uint8Array,
26+
AggregateError,
2627
} = primordials;
2728

2829
const { compare } = internalBinding('buffer');
@@ -235,9 +236,23 @@ function innerDeepEqual(val1, val2, strict, memos) {
235236
} else if (isNativeError(val1) || val1 instanceof Error) {
236237
// Do not compare the stack as it might differ even though the error itself
237238
// is otherwise identical.
238-
if ((!isNativeError(val2) && !(val2 instanceof Error)) ||
239-
val1.message !== val2.message ||
240-
val1.name !== val2.name) {
239+
if (!isNativeError(val2) && !(val2 instanceof Error)) {
240+
return false;
241+
}
242+
243+
const message1Enumerable = ObjectPrototypePropertyIsEnumerable(val1, 'message');
244+
const name1Enumerable = ObjectPrototypePropertyIsEnumerable(val1, 'name');
245+
const cause1Enumerable = ObjectPrototypePropertyIsEnumerable(val1, 'cause');
246+
const errors1Enumerable = ObjectPrototypePropertyIsEnumerable(val1, 'errors');
247+
248+
if ((message1Enumerable !== ObjectPrototypePropertyIsEnumerable(val2, 'message') ||
249+
(!message1Enumerable && val1.message !== val2.message)) ||
250+
(name1Enumerable !== ObjectPrototypePropertyIsEnumerable(val2, 'name') ||
251+
(!name1Enumerable && val1.name !== val2.name)) ||
252+
(cause1Enumerable !== ObjectPrototypePropertyIsEnumerable(val2, 'cause') ||
253+
(!cause1Enumerable && !innerDeepEqual(val1.cause, val2.cause, strict, memos))) ||
254+
(errors1Enumerable !== ObjectPrototypePropertyIsEnumerable(val2, 'errors') ||
255+
(!errors1Enumerable && !innerDeepEqual(val1.errors, val2.errors, strict, memos)))) {
241256
return false;
242257
}
243258
} else if (isBoxedPrimitive(val1)) {

test/parallel/test-assert-deep.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,29 @@ assert.throws(
11111111
assert.notDeepStrictEqual(err, err2);
11121112
}
11131113

1114+
// Check for Errors with cause property
1115+
{
1116+
const e1 = new Error('err', { cause: new Error('cause e1') });
1117+
const e2 = new Error('err', { cause: new Error('cause e2') });
1118+
assertNotDeepOrStrict(e1, e2, AssertionError);
1119+
assertNotDeepOrStrict(e1, new Error('err'), AssertionError);
1120+
assertDeepAndStrictEqual(e1, new Error('err', { cause: new Error('cause e1') }), AssertionError);
1121+
}
1122+
1123+
// Check for AggregateError
1124+
{
1125+
const e1 = new Error('e1');
1126+
const e1duplicate = new Error('e1');
1127+
const e2 = new Error('e2');
1128+
1129+
const e3 = new AggregateError([e1duplicate, e2], 'Aggregate Error');
1130+
const e3duplicate = new AggregateError([e1, e2], 'Aggregate Error');
1131+
const e4 = new AggregateError([e1], 'Aggregate Error');
1132+
assertNotDeepOrStrict(e1, e3, AssertionError);
1133+
assertNotDeepOrStrict(e3, e4, AssertionError);
1134+
assertDeepAndStrictEqual(e3, e3duplicate, AssertionError);
1135+
}
1136+
11141137
// Verify that `valueOf` is not called for boxed primitives.
11151138
{
11161139
const a = new Number(5);

0 commit comments

Comments
 (0)