assert: fix deepStrictEqual on errors when cause is not undefined

PR-URL: https://github.com/nodejs/node/pull/55406
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Edigleysson Silva (Edy) 2024-11-05 20:45:05 -03:00 committed by GitHub
parent ecc62381a8
commit 4da8d110f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 50 additions and 0 deletions

View File

@ -15,6 +15,8 @@ const {
StringPrototypeSplit,
} = primordials;
const { isError } = require('internal/util');
const { inspect } = require('internal/util/inspect');
const colors = require('internal/util/colors');
const { validateObject } = require('internal/validators');
@ -47,6 +49,15 @@ function copyError(source) {
__proto__: null,
value: source.message,
});
if (source.cause !== undefined) {
let cause = source.cause;
if (isError(cause)) {
cause = copyError(cause);
}
ObjectDefineProperty(target, 'cause', { __proto__: null, value: cause });
}
return target;
}

View File

@ -0,0 +1,39 @@
'use strict';
require('../common');
const assert = require('assert');
const { test } = require('node:test');
const defaultStartMessage = 'Expected values to be strictly deep-equal:\n' +
'+ actual - expected\n' +
'\n';
test('Handle error causes', () => {
assert.throws(() => {
assert.deepStrictEqual(new Error('a', { cause: new Error('x') }), new Error('a', { cause: new Error('y') }));
}, { message: defaultStartMessage + ' [Error: a] {\n' +
'+ [cause]: [Error: x]\n' +
'- [cause]: [Error: y]\n' +
' }\n' });
assert.throws(() => {
assert.deepStrictEqual(new Error('a'), new Error('a', { cause: new Error('y') }));
}, { message: defaultStartMessage + '+ [Error: a]\n' +
'- [Error: a] {\n' +
'- [cause]: [Error: y]\n' +
'- }\n' });
assert.throws(() => {
assert.deepStrictEqual(new Error('a'), new Error('a', { cause: { prop: 'value' } }));
}, { message: defaultStartMessage + '+ [Error: a]\n' +
'- [Error: a] {\n' +
'- [cause]: {\n' +
'- prop: \'value\'\n' +
'- }\n' +
'- }\n' });
assert.notDeepStrictEqual(new Error('a', { cause: new Error('x') }), new Error('a', { cause: new Error('y') }));
assert.notDeepStrictEqual(
new Error('a', { cause: { prop: 'value' } }),
new Error('a', { cause: { prop: 'a different value' } })
);
});