Skip to content

Commit 764b13d

Browse files
synapsenodejs-github-bot
authored andcommitted
assert,util: change WeakMap and WeakSet comparison handling
PR-URL: #53495 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
1 parent 9cd9aa8 commit 764b13d

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

doc/api/assert.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,8 @@ are also recursively evaluated by the following rules.
636636
* Implementation does not test the [`[[Prototype]]`][prototype-spec] of
637637
objects.
638638
* [`Symbol`][] properties are not compared.
639-
* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values.
639+
* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values
640+
but only on their instances.
640641
* [`RegExp`][] lastIndex, flags, and source are always compared, even if these
641642
are not enumerable properties.
642643

lib/internal/util/comparisons.js

+5
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 false;
286290
}
291+
287292
return keyCheck(val1, val2, strict, memos, kNoIterator);
288293
}
289294

test/parallel/test-assert-deep.js

+32
Original file line numberDiff line numberDiff line change
@@ -1304,3 +1304,35 @@ if (common.hasCrypto) {
13041304
}
13051305
})().then(common.mustCall());
13061306
}
1307+
1308+
// Comparing two identical WeakMap instances
1309+
{
1310+
const weakMap = new WeakMap();
1311+
assertDeepAndStrictEqual(weakMap, weakMap);
1312+
}
1313+
1314+
// Comparing two different WeakMap instances
1315+
{
1316+
const weakMap1 = new WeakMap();
1317+
const objA = {};
1318+
weakMap1.set(objA, 'ok');
1319+
1320+
const weakMap2 = new WeakMap();
1321+
const objB = {};
1322+
weakMap2.set(objB, 'ok');
1323+
1324+
assertNotDeepOrStrict(weakMap1, weakMap2);
1325+
}
1326+
1327+
// Comparing two identical WeakSet instances
1328+
{
1329+
const weakSet = new WeakSet();
1330+
assertDeepAndStrictEqual(weakSet, weakSet);
1331+
}
1332+
1333+
// Comparing two different WeakSet instances
1334+
{
1335+
const weakSet1 = new WeakSet();
1336+
const weakSet2 = new WeakSet();
1337+
assertNotDeepOrStrict(weakSet1, weakSet2);
1338+
}

0 commit comments

Comments
 (0)