Skip to content

Commit 69bd695

Browse files
committed
Fix WeakMap and WeakSet comparison handling
1 parent 1872167 commit 69bd695

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

doc/api/assert.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,8 @@ are also recursively evaluated by the following rules.
634634
* Implementation does not test the [`[[Prototype]]`][prototype-spec] of
635635
objects.
636636
* [`Symbol`][] properties are not compared.
637-
* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values.
637+
* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values
638+
but only on their instances.
638639
* [`RegExp`][] lastIndex, flags, and source are always compared, even if these
639640
are not enumerable properties.
640641

lib/internal/util/comparisons.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const {
4646
isFloat64Array,
4747
isKeyObject,
4848
isCryptoKey,
49+
isWeakMap,
50+
isWeakSet,
4951
} = types;
5052
const {
5153
constants: {
@@ -283,7 +285,10 @@ function innerDeepEqual(val1, val2, strict, memos) {
283285
) {
284286
return false;
285287
}
288+
} else if (isWeakMap(val1) || isWeakSet(val1)) {
289+
return val1 === val2;
286290
}
291+
287292
return keyCheck(val1, val2, strict, memos, kNoIterator);
288293
}
289294

test/parallel/test-assert-deep.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,3 +1300,43 @@ if (common.hasCrypto) {
13001300
}
13011301
})().then(common.mustCall());
13021302
}
1303+
1304+
// Comparing two identical WeakMap instances
1305+
{
1306+
const weakMap1 = new WeakMap();
1307+
const weakMap2 = weakMap1;
1308+
assert.deepStrictEqual(weakMap1, weakMap2);
1309+
}
1310+
1311+
// Comparing two different WeakMap instances
1312+
{
1313+
const weakMap1 = new WeakMap();
1314+
const objA = {};
1315+
weakMap1.set(objA, 'ok');
1316+
1317+
const weakMap2 = new WeakMap();
1318+
const objB = {};
1319+
weakMap2.set(objB, 'ok');
1320+
1321+
assert.throws(
1322+
() => assert.deepStrictEqual(weakMap1, weakMap2),
1323+
Error
1324+
);
1325+
}
1326+
1327+
// Comparing two identical WeakSet instances
1328+
{
1329+
const weakSet1 = new WeakSet();
1330+
const weakSet2 = weakSet1;
1331+
assert.deepStrictEqual(weakSet1, weakSet2);
1332+
}
1333+
1334+
// Comparing two different WeakSet instances
1335+
{
1336+
const weakSet1 = new WeakSet();
1337+
const weakSet2 = new WeakSet();
1338+
assert.throws(
1339+
() => assert.deepStrictEqual(weakSet1, weakSet2),
1340+
Error
1341+
);
1342+
}

0 commit comments

Comments
 (0)