diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index 6fe571d26e20a1..bffd7c827ce9eb 100644 --- a/lib/internal/assert/assertion_error.js +++ b/lib/internal/assert/assertion_error.js @@ -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'); @@ -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); } diff --git a/test/parallel/test-assert-deep-with-error.js b/test/parallel/test-assert-deep-with-error.js index 901fd7db0f6422..b4186eb642302d 100644 --- a/test/parallel/test-assert-deep-with-error.js +++ b/test/parallel/test-assert-deep-with-error.js @@ -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' } }) + ); });