Skip to content

Commit 9862912

Browse files
committed
assert: differentiate cases where cause is undefined or missing
PR-URL: #55738 Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 1568733 commit 9862912

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

lib/internal/assert/assertion_error.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
ObjectAssign,
1010
ObjectDefineProperty,
1111
ObjectGetPrototypeOf,
12+
ObjectPrototypeHasOwnProperty,
1213
String,
1314
StringPrototypeRepeat,
1415
StringPrototypeSlice,
@@ -49,8 +50,8 @@ function copyError(source) {
4950
__proto__: null,
5051
value: source.message,
5152
});
52-
if (source.cause !== undefined) {
53-
let cause = source.cause;
53+
if (ObjectPrototypeHasOwnProperty(source, 'cause')) {
54+
let { cause } = source;
5455

5556
if (isError(cause)) {
5657
cause = copyError(cause);

lib/internal/util/comparisons.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const {
2828
const { compare } = internalBinding('buffer');
2929
const assert = require('internal/assert');
3030
const { isURL } = require('internal/url');
31+
const { isError } = require('internal/util');
3132
const types = require('internal/util/types');
3233
const {
3334
isAnyArrayBuffer,
@@ -235,10 +236,10 @@ function innerDeepEqual(val1, val2, strict, memos) {
235236
if (!isAnyArrayBuffer(val2) || !areEqualArrayBuffers(val1, val2)) {
236237
return false;
237238
}
238-
} else if (isNativeError(val1) || val1 instanceof Error) {
239+
} else if (isError(val1)) {
239240
// Do not compare the stack as it might differ even though the error itself
240241
// is otherwise identical.
241-
if (!isNativeError(val2) && !(val2 instanceof Error)) {
242+
if (!isError(val2)) {
242243
return false;
243244
}
244245

@@ -252,7 +253,9 @@ function innerDeepEqual(val1, val2, strict, memos) {
252253
(name1Enumerable !== ObjectPrototypePropertyIsEnumerable(val2, 'name') ||
253254
(!name1Enumerable && val1.name !== val2.name)) ||
254255
(cause1Enumerable !== ObjectPrototypePropertyIsEnumerable(val2, 'cause') ||
255-
(!cause1Enumerable && !innerDeepEqual(val1.cause, val2.cause, strict, memos))) ||
256+
(!cause1Enumerable && (
257+
ObjectPrototypeHasOwnProperty(val1, 'cause') !== ObjectPrototypeHasOwnProperty(val2, 'cause') ||
258+
!innerDeepEqual(val1.cause, val2.cause, strict, memos)))) ||
256259
(errors1Enumerable !== ObjectPrototypePropertyIsEnumerable(val2, 'errors') ||
257260
(!errors1Enumerable && !innerDeepEqual(val1.errors, val2.errors, strict, memos)))) {
258261
return false;

test/parallel/test-assert-deep-with-error.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,26 @@ const defaultStartMessage = 'Expected values to be strictly deep-equal:\n' +
88
'\n';
99

1010
test('Handle error causes', () => {
11+
assert.deepStrictEqual(new Error('a', { cause: new Error('x') }), new Error('a', { cause: new Error('x') }));
12+
assert.deepStrictEqual(
13+
new Error('a', { cause: new RangeError('x') }),
14+
new Error('a', { cause: new RangeError('x') }),
15+
);
16+
1117
assert.throws(() => {
1218
assert.deepStrictEqual(new Error('a', { cause: new Error('x') }), new Error('a', { cause: new Error('y') }));
1319
}, { message: defaultStartMessage + ' [Error: a] {\n' +
1420
'+ [cause]: [Error: x]\n' +
1521
'- [cause]: [Error: y]\n' +
1622
' }\n' });
1723

24+
assert.throws(() => {
25+
assert.deepStrictEqual(new Error('a', { cause: new Error('x') }), new Error('a', { cause: new TypeError('x') }));
26+
}, { message: defaultStartMessage + ' [Error: a] {\n' +
27+
'+ [cause]: [Error: x]\n' +
28+
'- [cause]: [TypeError: x]\n' +
29+
' }\n' });
30+
1831
assert.throws(() => {
1932
assert.deepStrictEqual(new Error('a'), new Error('a', { cause: new Error('y') }));
2033
}, { message: defaultStartMessage + '+ [Error: a]\n' +
@@ -37,3 +50,33 @@ test('Handle error causes', () => {
3750
new Error('a', { cause: { prop: 'a different value' } })
3851
);
3952
});
53+
54+
test('Handle undefined causes', () => {
55+
assert.deepStrictEqual(new Error('a', { cause: undefined }), new Error('a', { cause: undefined }));
56+
57+
assert.notDeepStrictEqual(new Error('a', { cause: 'undefined' }), new Error('a', { cause: undefined }));
58+
assert.notDeepStrictEqual(new Error('a', { cause: undefined }), new Error('a'));
59+
assert.notDeepStrictEqual(new Error('a'), new Error('a', { cause: undefined }));
60+
61+
assert.throws(() => {
62+
assert.deepStrictEqual(new Error('a'), new Error('a', { cause: undefined }));
63+
}, { message: defaultStartMessage +
64+
'+ [Error: a]\n' +
65+
'- [Error: a] {\n' +
66+
'- [cause]: undefined\n' +
67+
'- }\n' });
68+
69+
assert.throws(() => {
70+
assert.deepStrictEqual(new Error('a', { cause: undefined }), new Error('a'));
71+
}, { message: defaultStartMessage +
72+
'+ [Error: a] {\n' +
73+
'+ [cause]: undefined\n' +
74+
'+ }\n' +
75+
'- [Error: a]\n' });
76+
assert.throws(() => {
77+
assert.deepStrictEqual(new Error('a', { cause: undefined }), new Error('a', { cause: 'undefined' }));
78+
}, { message: defaultStartMessage + ' [Error: a] {\n' +
79+
'+ [cause]: undefined\n' +
80+
'- [cause]: \'undefined\'\n' +
81+
' }\n' });
82+
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy