Skip to content

Commit

Permalink
Fix WeakMap and WeakSet comparison handling
Browse files Browse the repository at this point in the history
  • Loading branch information
synapse committed Jun 18, 2024
1 parent 1872167 commit 69bd695
Show file tree
Hide file tree
Showing 3 changed files with 47 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 @@ -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 val1 === val2;
}

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

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

// Comparing two identical WeakMap instances
{
const weakMap1 = new WeakMap();
const weakMap2 = weakMap1;
assert.deepStrictEqual(weakMap1, weakMap2);
}

// 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');

assert.throws(
() => assert.deepStrictEqual(weakMap1, weakMap2),
Error
);
}

// Comparing two identical WeakSet instances
{
const weakSet1 = new WeakSet();
const weakSet2 = weakSet1;
assert.deepStrictEqual(weakSet1, weakSet2);
}

// Comparing two different WeakSet instances
{
const weakSet1 = new WeakSet();
const weakSet2 = new WeakSet();
assert.throws(
() => assert.deepStrictEqual(weakSet1, weakSet2),
Error
);
}

0 comments on commit 69bd695

Please sign in to comment.