From f1d2d677443105c8ba2851d410432e329de878bd Mon Sep 17 00:00:00 2001 From: Edy Silva Date: Sat, 19 Oct 2024 16:29:54 -0300 Subject: [PATCH 1/2] assert: fix deepStrictEqual on errors when cause is present --- lib/internal/assert/assertion_error.js | 9 +++++++ test/parallel/test-assert-deep-with-error.js | 26 ++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 test/parallel/test-assert-deep-with-error.js diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index ee376899d04d35..6fe571d26e20a1 100644 --- a/lib/internal/assert/assertion_error.js +++ b/lib/internal/assert/assertion_error.js @@ -47,6 +47,15 @@ function copyError(source) { __proto__: null, value: source.message, }); + if (source.cause !== undefined) { + let cause = source.cause; + + if (cause instanceof Error) { + cause = copyError(cause); + } + + ObjectDefineProperty(target, 'cause', { __proto__: null, value: cause }); + } return target; } diff --git a/test/parallel/test-assert-deep-with-error.js b/test/parallel/test-assert-deep-with-error.js new file mode 100644 index 00000000000000..901fd7db0f6422 --- /dev/null +++ b/test/parallel/test-assert-deep-with-error.js @@ -0,0 +1,26 @@ +'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' + + ' }' }); + + 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' + + '- }' }); + + assert.notDeepStrictEqual(new Error('a', { cause: new Error('x') }), new Error('a', { cause: new Error('y') })); +}); From 5ac53d26f99de110a2034d1d06c2e52781d68f68 Mon Sep 17 00:00:00 2001 From: Edy Silva Date: Mon, 28 Oct 2024 10:18:31 -0300 Subject: [PATCH 2/2] test: test when cause is not an instance of Error --- lib/internal/assert/assertion_error.js | 4 +++- test/parallel/test-assert-deep-with-error.js | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index 6fe571d26e20a1..68633278eb10e6 100644 --- a/lib/internal/assert/assertion_error.js +++ b/lib/internal/assert/assertion_error.js @@ -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'); @@ -50,7 +52,7 @@ function copyError(source) { if (source.cause !== undefined) { let cause = source.cause; - if (cause instanceof Error) { + if (isError(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' } }) + ); });