diff --git a/doc/api/assert.md b/doc/api/assert.md index eea4d67ac9626d..45e8efe3be3ebf 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -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. diff --git a/lib/internal/util/comparisons.js b/lib/internal/util/comparisons.js index 523307aa29851b..c98199f7c376a6 100644 --- a/lib/internal/util/comparisons.js +++ b/lib/internal/util/comparisons.js @@ -46,6 +46,8 @@ const { isFloat64Array, isKeyObject, isCryptoKey, + isWeakMap, + isWeakSet, } = types; const { constants: { @@ -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); } diff --git a/test/parallel/test-assert-deep.js b/test/parallel/test-assert-deep.js index e94be7af1e6f23..0711d4b7ea496c 100644 --- a/test/parallel/test-assert-deep.js +++ b/test/parallel/test-assert-deep.js @@ -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); +}