From 24ed8da48ee11b0d4130f1c71a448db59c83f607 Mon Sep 17 00:00:00 2001 From: Giovanni Bucci Date: Thu, 9 Jan 2025 18:35:54 +0100 Subject: [PATCH] assert: make partialDeepStrictEqual work with urls and File prototypes PR-URL: https://github.com/nodejs/node/pull/56231 Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Xuguang Mei Reviewed-By: Pietro Marchini --- lib/assert.js | 9 +++++-- test/parallel/test-assert-objects.js | 25 +++++++++++++++++++ .../test-assert-typedarray-deepequal.js | 5 ++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 16c06593601eac..603f2a026313c9 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -95,6 +95,7 @@ const CallTracker = require('internal/assert/calltracker'); const { validateFunction, } = require('internal/validators'); +const { isURL } = require('internal/url'); let isDeepEqual; let isDeepStrictEqual; @@ -383,7 +384,7 @@ function isSpecial(obj) { } const typesToCallDeepStrictEqualWith = [ - isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer, isSharedArrayBuffer, + isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer, isSharedArrayBuffer, isURL, ]; function partiallyCompareMaps(actual, expected, comparedObjects) { @@ -466,7 +467,7 @@ function partiallyCompareArrayBuffersOrViews(actual, expected) { } for (let i = 0; i < expectedViewLength; i++) { - if (actualView[i] !== expectedView[i]) { + if (!ObjectIs(actualView[i], expectedView[i])) { return false; } } @@ -586,6 +587,10 @@ function compareBranch( expected, comparedObjects, ) { + // Checking for the simplest case possible. + if (actual === expected) { + return true; + } // Check for Map object equality if (isMap(actual) && isMap(expected)) { return partiallyCompareMaps(actual, expected, comparedObjects); diff --git a/test/parallel/test-assert-objects.js b/test/parallel/test-assert-objects.js index 43fd38a43c3c0b..4cf8f424f91913 100644 --- a/test/parallel/test-assert-objects.js +++ b/test/parallel/test-assert-objects.js @@ -306,6 +306,16 @@ describe('Object Comparison Tests', () => { actual: { dataView: new Uint8Array(3) }, expected: { dataView: new DataView(new ArrayBuffer(3)) }, }, + { + description: 'throws when comparing Float32Array([+0.0]) with Float32Array([-0.0])', + actual: new Float32Array([+0.0]), + expected: new Float32Array([-0.0]), + }, + { + description: 'throws when comparing two different urls', + actual: new URL('http://foo'), + expected: new URL('http://bar'), + }, { description: 'throws when comparing SharedArrayBuffers when expected has different elements actual', actual: (() => { @@ -778,6 +788,21 @@ describe('Object Comparison Tests', () => { actual: [1, 2, 3], expected: [2], }, + { + description: 'ensures that File extends Blob', + actual: Object.getPrototypeOf(File.prototype), + expected: Blob.prototype + }, + { + description: 'compares NaN with NaN', + actual: NaN, + expected: NaN, + }, + { + description: 'compares two identical urls', + actual: new URL('http://foo'), + expected: new URL('http://foo'), + }, ].forEach(({ description, actual, expected }) => { it(description, () => { assert.partialDeepStrictEqual(actual, expected); diff --git a/test/parallel/test-assert-typedarray-deepequal.js b/test/parallel/test-assert-typedarray-deepequal.js index 7fb18c1886ba91..403cd6748d507e 100644 --- a/test/parallel/test-assert-typedarray-deepequal.js +++ b/test/parallel/test-assert-typedarray-deepequal.js @@ -101,10 +101,15 @@ suite('notEqualArrayPairs', () => { makeBlock(assert.deepStrictEqual, arrayPair[0], arrayPair[1]), assert.AssertionError ); + // TODO(puskin94): remove emitWarning override once the partialDeepStrictEqual method is not experimental anymore + // Suppress warnings, necessary otherwise the tools/pseudo-tty.py runner will fail + const originalEmitWarning = process.emitWarning; + process.emitWarning = () => {}; assert.throws( makeBlock(assert.partialDeepStrictEqual, arrayPair[0], arrayPair[1]), assert.AssertionError ); + process.emitWarning = originalEmitWarning; // Restore original process.emitWarning }); } });