Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert,util: change WeakMap and WeakSet comparison handling #53495

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,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 @@ -1300,3 +1300,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);
}