Skip to content

Commit

Permalink
assert,util: change WeakMap and WeakSet comparison handling
Browse files Browse the repository at this point in the history
PR-URL: nodejs#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>
  • Loading branch information
synapse authored and nodejs-github-bot committed Jun 28, 2024
1 parent 9cd9aa8 commit 764b13d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion doc/api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,8 @@ are also recursively evaluated by the following rules.
* Implementation does not test the [`[[Prototype]]`][prototype-spec] of
objects.
* [`Symbol`][] properties are not compared.
* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values.
* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values
but only on their instances.
* [`RegExp`][] lastIndex, flags, and source are always compared, even if these
are not enumerable properties.

Expand Down
5 changes: 5 additions & 0 deletions lib/internal/util/comparisons.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const {
isFloat64Array,
isKeyObject,
isCryptoKey,
isWeakMap,
isWeakSet,
} = types;
const {
constants: {
Expand Down Expand Up @@ -283,7 +285,10 @@ function innerDeepEqual(val1, val2, strict, memos) {
) {
return false;
}
} else if (isWeakMap(val1) || isWeakSet(val1)) {
return false;
}

return keyCheck(val1, val2, strict, memos, kNoIterator);
}

Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -1304,3 +1304,35 @@ if (common.hasCrypto) {
}
})().then(common.mustCall());
}

// Comparing two identical WeakMap instances
{
const weakMap = new WeakMap();
assertDeepAndStrictEqual(weakMap, weakMap);
}

// Comparing two different WeakMap instances
{
const weakMap1 = new WeakMap();
const objA = {};
weakMap1.set(objA, 'ok');

const weakMap2 = new WeakMap();
const objB = {};
weakMap2.set(objB, 'ok');

assertNotDeepOrStrict(weakMap1, weakMap2);
}

// Comparing two identical WeakSet instances
{
const weakSet = new WeakSet();
assertDeepAndStrictEqual(weakSet, weakSet);
}

// Comparing two different WeakSet instances
{
const weakSet1 = new WeakSet();
const weakSet2 = new WeakSet();
assertNotDeepOrStrict(weakSet1, weakSet2);
}

0 comments on commit 764b13d

Please sign in to comment.