Skip to content

Commit 749daee

Browse files
committed
Added WeakMap and WeakSet comparison handling
1 parent 1872167 commit 749daee

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

doc/api/assert.md

+2-1
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

+5
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ function innerDeepEqual(val1, val2, strict, memos) {
173173
return false;
174174
}
175175

176+
// Handle WeakMap and WeakSet
177+
if (val1Tag === '[object WeakMap]' || val1Tag === '[object WeakSet]') {
178+
return val1 === val2;
179+
}
180+
176181
if (ArrayIsArray(val1)) {
177182
// Check for sparse arrays and general fast path
178183
if (!ArrayIsArray(val2) || val1.length !== val2.length) {

test/parallel/test-assert-deep.js

+41
Original file line numberDiff line numberDiff line change
@@ -1300,3 +1300,44 @@ 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+
1339+
assert.throws(
1340+
() => assert.deepStrictEqual(weakSet1, weakSet2),
1341+
Error
1342+
);
1343+
}

0 commit comments

Comments
 (0)