Skip to content

Commit e222e36

Browse files
puskin94aduh95
authored andcommitted
assert: make partialDeepStrictEqual work with urls and File prototypes
PR-URL: #56231 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
1 parent 4844fa2 commit e222e36

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

lib/assert.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const CallTracker = require('internal/assert/calltracker');
9595
const {
9696
validateFunction,
9797
} = require('internal/validators');
98+
const { isURL } = require('internal/url');
9899

99100
let isDeepEqual;
100101
let isDeepStrictEqual;
@@ -383,7 +384,7 @@ function isSpecial(obj) {
383384
}
384385

385386
const typesToCallDeepStrictEqualWith = [
386-
isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer, isSharedArrayBuffer,
387+
isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer, isSharedArrayBuffer, isURL,
387388
];
388389

389390
function partiallyCompareMaps(actual, expected, comparedObjects) {
@@ -466,7 +467,7 @@ function partiallyCompareArrayBuffersOrViews(actual, expected) {
466467
}
467468

468469
for (let i = 0; i < expectedViewLength; i++) {
469-
if (actualView[i] !== expectedView[i]) {
470+
if (!ObjectIs(actualView[i], expectedView[i])) {
470471
return false;
471472
}
472473
}
@@ -586,6 +587,10 @@ function compareBranch(
586587
expected,
587588
comparedObjects,
588589
) {
590+
// Checking for the simplest case possible.
591+
if (actual === expected) {
592+
return true;
593+
}
589594
// Check for Map object equality
590595
if (isMap(actual) && isMap(expected)) {
591596
return partiallyCompareMaps(actual, expected, comparedObjects);

test/parallel/test-assert-objects.js

+25
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,16 @@ describe('Object Comparison Tests', () => {
296296
actual: { dataView: new Uint8Array(3) },
297297
expected: { dataView: new DataView(new ArrayBuffer(3)) },
298298
},
299+
{
300+
description: 'throws when comparing Float32Array([+0.0]) with Float32Array([-0.0])',
301+
actual: new Float32Array([+0.0]),
302+
expected: new Float32Array([-0.0]),
303+
},
304+
{
305+
description: 'throws when comparing two different urls',
306+
actual: new URL('http://foo'),
307+
expected: new URL('http://bar'),
308+
},
299309
{
300310
description: 'throws when comparing SharedArrayBuffers when expected has different elements actual',
301311
actual: (() => {
@@ -768,6 +778,21 @@ describe('Object Comparison Tests', () => {
768778
actual: [1, 2, 3],
769779
expected: [2],
770780
},
781+
{
782+
description: 'ensures that File extends Blob',
783+
actual: Object.getPrototypeOf(File.prototype),
784+
expected: Blob.prototype
785+
},
786+
{
787+
description: 'compares NaN with NaN',
788+
actual: NaN,
789+
expected: NaN,
790+
},
791+
{
792+
description: 'compares two identical urls',
793+
actual: new URL('http://foo'),
794+
expected: new URL('http://foo'),
795+
},
771796
].forEach(({ description, actual, expected }) => {
772797
it(description, () => {
773798
assert.partialDeepStrictEqual(actual, expected);

test/parallel/test-assert-typedarray-deepequal.js

+5
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,15 @@ suite('notEqualArrayPairs', () => {
101101
makeBlock(assert.deepStrictEqual, arrayPair[0], arrayPair[1]),
102102
assert.AssertionError
103103
);
104+
// TODO(puskin94): remove emitWarning override once the partialDeepStrictEqual method is not experimental anymore
105+
// Suppress warnings, necessary otherwise the tools/pseudo-tty.py runner will fail
106+
const originalEmitWarning = process.emitWarning;
107+
process.emitWarning = () => {};
104108
assert.throws(
105109
makeBlock(assert.partialDeepStrictEqual, arrayPair[0], arrayPair[1]),
106110
assert.AssertionError
107111
);
112+
process.emitWarning = originalEmitWarning; // Restore original process.emitWarning
108113
});
109114
}
110115
});

0 commit comments

Comments
 (0)