From 2d0f7aba7940d46e9d69bb06fa0e514bb0d192d0 Mon Sep 17 00:00:00 2001 From: "Edigleysson Silva (Edy)" Date: Tue, 5 Nov 2024 20:45:05 -0300 Subject: [PATCH] assert: fix `deepStrictEqual` on errors when `cause` is not undefined MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/55406 Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel Reviewed-By: Ulises Gascón Reviewed-By: Ruben Bridgewater --- lib/internal/assert/assertion_error.js | 11 ++++++ test/parallel/test-assert-deep-with-error.js | 39 ++++++++++++++++++++ 2 files changed, 50 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 dd56811647e5bf..5b4124e6b29700 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'); @@ -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; } 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..b4186eb642302d --- /dev/null +++ b/test/parallel/test-assert-deep-with-error.js @@ -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' } }) + ); +});