mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
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:
parent
ecc62381a8
commit
4da8d110f3
@ -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;
|
||||
}
|
||||
|
||||
|
39
test/parallel/test-assert-deep-with-error.js
Normal file
39
test/parallel/test-assert-deep-with-error.js
Normal 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' } })
|
||||
);
|
||||
});
|
Loading…
Reference in New Issue
Block a user