From 163ef7c46a788031570a7f989c9891cb07529728 Mon Sep 17 00:00:00 2001 From: Cristian Barlutiu Date: Tue, 18 Jun 2024 08:18:40 +0200 Subject: [PATCH] assert,util: change WeakMap and WeakSet comparison handling --- doc/api/assert.md | 3 ++- lib/internal/util/comparisons.js | 5 +++++ test/parallel/test-assert-deep.js | 32 +++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/doc/api/assert.md b/doc/api/assert.md index ec2771f4fe80b9..237e74161110ae 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -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. diff --git a/lib/internal/util/comparisons.js b/lib/internal/util/comparisons.js index b5d2d071479dec..51f40134213f78 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 28f6bedfb8f54f..d6f86dbc159d60 100644 --- a/test/parallel/test-assert-deep.js +++ b/test/parallel/test-assert-deep.js @@ -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); +}