Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert: make partialDeepStrictEqual work with urls and File prototypes #56231

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const CallTracker = require('internal/assert/calltracker');
const {
validateFunction,
} = require('internal/validators');
const { isURL } = require('internal/url');

let isDeepEqual;
let isDeepStrictEqual;
Expand Down Expand Up @@ -382,7 +383,7 @@ function isSpecial(obj) {
}

const typesToCallDeepStrictEqualWith = [
isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer, isSharedArrayBuffer,
isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer, isSharedArrayBuffer, isURL,
];

function partiallyCompareMaps(actual, expected, comparedObjects) {
Expand Down Expand Up @@ -465,7 +466,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;
}
}
Expand Down Expand Up @@ -555,6 +556,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);
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-assert-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,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: (() => {
Expand Down Expand Up @@ -728,6 +738,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);
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-assert-typedarray-deepequal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
});
Loading