Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e902477

Browse filesBrowse files
Giovanni Bucciruyadorno
authored andcommitted
assert: make Maps be partially compared in partialDeepStrictEqual
PR-URL: #56195 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 7cbe3de commit e902477
Copy full SHA for e902477

File tree

Expand file treeCollapse file tree

2 files changed

+84
-9
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+84
-9
lines changed
Open diff view settings
Collapse file

‎lib/assert.js‎

Copy file name to clipboardExpand all lines: lib/assert.js
+11-7Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -385,22 +385,26 @@ const typesToCallDeepStrictEqualWith = [
385385
isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer, isSharedArrayBuffer,
386386
];
387387

388-
function compareMaps(actual, expected, comparedObjects) {
389-
if (MapPrototypeGetSize(actual) !== MapPrototypeGetSize(expected)) {
388+
function partiallyCompareMaps(actual, expected, comparedObjects) {
389+
if (MapPrototypeGetSize(expected) > MapPrototypeGetSize(actual)) {
390390
return false;
391391
}
392-
const safeIterator = FunctionPrototypeCall(SafeMap.prototype[SymbolIterator], actual);
393392

394393
comparedObjects ??= new SafeWeakSet();
394+
const expectedIterator = FunctionPrototypeCall(SafeMap.prototype[SymbolIterator], expected);
395395

396-
for (const { 0: key, 1: val } of safeIterator) {
397-
if (!MapPrototypeHas(expected, key)) {
396+
for (const { 0: key, 1: expectedValue } of expectedIterator) {
397+
if (!MapPrototypeHas(actual, key)) {
398398
return false;
399399
}
400-
if (!compareBranch(val, MapPrototypeGet(expected, key), comparedObjects)) {
400+
401+
const actualValue = MapPrototypeGet(actual, key);
402+
403+
if (!compareBranch(actualValue, expectedValue, comparedObjects)) {
401404
return false;
402405
}
403406
}
407+
404408
return true;
405409
}
406410

@@ -553,7 +557,7 @@ function compareBranch(
553557
) {
554558
// Check for Map object equality
555559
if (isMap(actual) && isMap(expected)) {
556-
return compareMaps(actual, expected, comparedObjects);
560+
return partiallyCompareMaps(actual, expected, comparedObjects);
557561
}
558562

559563
if (
Collapse file

‎test/parallel/test-assert-objects.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-assert-objects.js
+73-2Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,26 @@ describe('Object Comparison Tests', () => {
159159
},
160160
{
161161
description:
162-
'throws when comparing two Map objects with different length',
162+
'throws when the expected Map has more entries than the actual Map',
163163
actual: new Map([
164164
['key1', 'value1'],
165165
['key2', 'value2'],
166166
]),
167-
expected: new Map([['key1', 'value1']]),
167+
expected: new Map([
168+
['key1', 'value1'],
169+
['key2', 'value2'],
170+
['key3', 'value3'],
171+
]),
172+
},
173+
{
174+
description: 'throws when the nested array in the Map is not a subset of the other nested array',
175+
actual: new Map([
176+
['key1', ['value1', 'value2']],
177+
['key2', 'value2'],
178+
]),
179+
expected: new Map([
180+
['key1', ['value3']],
181+
]),
168182
},
169183
{
170184
description:
@@ -554,6 +568,63 @@ describe('Object Comparison Tests', () => {
554568
['key2', 'value2'],
555569
]),
556570
},
571+
{
572+
description:
573+
'compares two Map objects where expected is a subset of actual',
574+
actual: new Map([
575+
['key1', 'value1'],
576+
['key2', 'value2'],
577+
]),
578+
expected: new Map([['key1', 'value1']]),
579+
},
580+
{
581+
description:
582+
'compares two deeply nested Maps',
583+
actual: {
584+
a: {
585+
b: {
586+
c: new Map([
587+
['key1', 'value1'],
588+
['key2', 'value2'],
589+
])
590+
},
591+
z: [1, 2, 3]
592+
}
593+
},
594+
expected: {
595+
a: {
596+
z: [1, 2, 3],
597+
b: {
598+
c: new Map([['key1', 'value1']])
599+
}
600+
}
601+
},
602+
},
603+
{
604+
description: 'compares Maps nested into Maps',
605+
actual: new Map([
606+
['key1', new Map([
607+
['nestedKey1', 'nestedValue1'],
608+
['nestedKey2', 'nestedValue2'],
609+
])],
610+
['key2', 'value2'],
611+
]),
612+
expected: new Map([
613+
['key1', new Map([
614+
['nestedKey1', 'nestedValue1'],
615+
])],
616+
])
617+
},
618+
{
619+
description: 'compares Maps with nested arrays inside',
620+
actual: new Map([
621+
['key1', ['value1', 'value2']],
622+
['key2', 'value2'],
623+
]),
624+
expected: new Map([
625+
['key1', ['value1', 'value2']],
626+
]),
627+
},
557628
{
558629
description:
559630
'compares two objects with identical getter/setter properties',

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.