From 03d453e14349a98967260508c5fc3d425e6c2f4b Mon Sep 17 00:00:00 2001 From: Shreyans Pathak Date: Fri, 17 Jan 2025 15:33:58 -0500 Subject: [PATCH 1/3] doc: `WeakSet` and `WeakMap` comparison details --- doc/api/assert.md | 81 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 24 deletions(-) diff --git a/doc/api/assert.md b/doc/api/assert.md index 3f44dffbe7de2d..63912daf6217be 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -804,8 +804,7 @@ are recursively evaluated also by the following rules. * [`Map`][] keys and [`Set`][] items are compared unordered. * Recursion stops when both sides differ or both sides encounter a circular reference. -* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values. See - below for further details. +* [`WeakMap`][] and [`WeakSet`][] instances are **not** compared structurally. They are only equal if they reference the same object. Any comparison between different `WeakMap` or `WeakSet` instances will result in inequality, even if they contain the same entries. * [`RegExp`][] lastIndex, flags, and source are always compared, even if these are not enumerable properties. @@ -882,23 +881,40 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 }); // } const weakMap1 = new WeakMap(); -const weakMap2 = new WeakMap([[{}, {}]]); -const weakMap3 = new WeakMap(); -weakMap3.unequal = true; +const weakMap2 = new WeakMap(); +const obj = {}; +weakMap1.set(obj, 'value'); +weakMap2.set(obj, 'value'); + +// Comparing different instances fails, even with same contents assert.deepStrictEqual(weakMap1, weakMap2); -// OK, because it is impossible to compare the entries +// AssertionError: Values have same structure but are not reference-equal: +// +// WeakMap { +// +// } + +// Comparing the same instance to itself succeeds +assert.deepStrictEqual(weakMap1, weakMap1); +// OK -// Fails because weakMap3 has a property that weakMap1 does not contain: -assert.deepStrictEqual(weakMap1, weakMap3); +const weakSet1 = new WeakSet(); +const weakSet2 = new WeakSet(); +weakSet1.add(obj); +weakSet2.add(obj); + +// Comparing different instances fails, even with same contents +assert.deepStrictEqual(weakSet1, weakSet2); // AssertionError: Expected inputs to be strictly deep-equal: // + actual - expected // -// WeakMap { -// + [items unknown] -// - [items unknown], -// - unequal: true -// } +// + WeakSet { } +// - WeakSet { } + +// Comparing the same instance to itself succeeds +assert.deepStrictEqual(weakSet1, weakSet1); +// OK ``` ```cjs @@ -974,23 +990,40 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 }); // } const weakMap1 = new WeakMap(); -const weakMap2 = new WeakMap([[{}, {}]]); -const weakMap3 = new WeakMap(); -weakMap3.unequal = true; +const weakMap2 = new WeakMap(); +const obj = {}; +weakMap1.set(obj, 'value'); +weakMap2.set(obj, 'value'); + +// Comparing different instances fails, even with same contents assert.deepStrictEqual(weakMap1, weakMap2); -// OK, because it is impossible to compare the entries +// AssertionError: Values have same structure but are not reference-equal: +// +// WeakMap { +// +// } + +// Comparing the same instance to itself succeeds +assert.deepStrictEqual(weakMap1, weakMap1); +// OK -// Fails because weakMap3 has a property that weakMap1 does not contain: -assert.deepStrictEqual(weakMap1, weakMap3); +const weakSet1 = new WeakSet(); +const weakSet2 = new WeakSet(); +weakSet1.add(obj); +weakSet2.add(obj); + +// Comparing different instances fails, even with same contents +assert.deepStrictEqual(weakSet1, weakSet2); // AssertionError: Expected inputs to be strictly deep-equal: // + actual - expected // -// WeakMap { -// + [items unknown] -// - [items unknown], -// - unequal: true -// } +// + WeakSet { } +// - WeakSet { } + +// Comparing the same instance to itself succeeds +assert.deepStrictEqual(weakSet1, weakSet1); +// OK ``` If the values are not equal, an [`AssertionError`][] is thrown with a `message` From 17a65b85ef6706b289eaf34b8b3fe86f4d1ee439 Mon Sep 17 00:00:00 2001 From: Shreyans Pathak Date: Sat, 18 Jan 2025 09:18:22 -0500 Subject: [PATCH 2/3] fix: linter --- doc/api/assert.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/doc/api/assert.md b/doc/api/assert.md index 63912daf6217be..d55ab729c31aec 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -804,7 +804,10 @@ are recursively evaluated also by the following rules. * [`Map`][] keys and [`Set`][] items are compared unordered. * Recursion stops when both sides differ or both sides encounter a circular reference. -* [`WeakMap`][] and [`WeakSet`][] instances are **not** compared structurally. They are only equal if they reference the same object. Any comparison between different `WeakMap` or `WeakSet` instances will result in inequality, even if they contain the same entries. +* [`WeakMap`][] and [`WeakSet`][] instances are **not** compared structurally. +They are only equal if they reference the same object. Any comparison between +different `WeakMap` or `WeakSet` instances will result in inequality, +even if they contain the same entries. * [`RegExp`][] lastIndex, flags, and source are always compared, even if these are not enumerable properties. From 917ef77faafa8940a7cfe3f5c7a864c2d1bbee83 Mon Sep 17 00:00:00 2001 From: Shreyans Pathak Date: Sat, 18 Jan 2025 09:32:14 -0500 Subject: [PATCH 3/3] fix: run formatter --- doc/api/assert.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/api/assert.md b/doc/api/assert.md index d55ab729c31aec..32740d2f303a8d 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -805,9 +805,9 @@ are recursively evaluated also by the following rules. * Recursion stops when both sides differ or both sides encounter a circular reference. * [`WeakMap`][] and [`WeakSet`][] instances are **not** compared structurally. -They are only equal if they reference the same object. Any comparison between -different `WeakMap` or `WeakSet` instances will result in inequality, -even if they contain the same entries. + They are only equal if they reference the same object. Any comparison between + different `WeakMap` or `WeakSet` instances will result in inequality, + even if they contain the same entries. * [`RegExp`][] lastIndex, flags, and source are always compared, even if these are not enumerable properties.