Skip to content

Commit

Permalink
test: test when cause is not an instance of Error
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksilva97 committed Oct 20, 2024
1 parent d55ba4f commit 055d6a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/internal/assert/assertion_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const {
StringPrototypeSplit,
} = primordials;

const types = require('internal/util/types');
const { isNativeError } = types;

const { inspect } = require('internal/util/inspect');
const colors = require('internal/util/colors');
const { validateObject } = require('internal/validators');
Expand Down Expand Up @@ -50,7 +53,7 @@ function copyError(source) {
if (source.cause !== undefined) {
let cause = source.cause;

if (cause instanceof Error) {
if (isNativeError(cause)) {
cause = copyError(cause);
}

Expand Down
17 changes: 15 additions & 2 deletions test/parallel/test-assert-deep-with-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,27 @@ test('Handle error causes', () => {
}, { 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' } })
);
});

0 comments on commit 055d6a6

Please sign in to comment.