@@ -799,8 +799,10 @@ are recursively evaluated also by the following rules.
799799* [`Map`][] keys and [`Set`][] items are compared unordered.
800800* Recursion stops when both sides differ or both sides encounter a circular
801801 reference.
802- * [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values. See
803- below for further details.
802+ * [`WeakMap`][] and [`WeakSet`][] instances are **not** compared structurally.
803+ They are only equal if they reference the same object. Any comparison between
804+ different `WeakMap` or `WeakSet` instances will result in inequality,
805+ even if they contain the same entries.
804806* [`RegExp`][] lastIndex, flags, and source are always compared, even if these
805807 are not enumerable properties.
806808
@@ -877,23 +879,40 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
877879// }
878880
879881const weakMap1 = new WeakMap();
880- const weakMap2 = new WeakMap([[{}, {}]]);
881- const weakMap3 = new WeakMap();
882- weakMap3.unequal = true;
882+ const weakMap2 = new WeakMap();
883+ const obj = {};
883884
885+ weakMap1.set(obj, 'value');
886+ weakMap2.set(obj, 'value');
887+
888+ // Comparing different instances fails, even with same contents
884889assert.deepStrictEqual(weakMap1, weakMap2);
885- // OK, because it is impossible to compare the entries
890+ // AssertionError: Values have same structure but are not reference-equal:
891+ //
892+ // WeakMap {
893+ // <items unknown>
894+ // }
895+
896+ // Comparing the same instance to itself succeeds
897+ assert.deepStrictEqual(weakMap1, weakMap1);
898+ // OK
886899
887- // Fails because weakMap3 has a property that weakMap1 does not contain:
888- assert.deepStrictEqual(weakMap1, weakMap3);
900+ const weakSet1 = new WeakSet();
901+ const weakSet2 = new WeakSet();
902+ weakSet1.add(obj);
903+ weakSet2.add(obj);
904+
905+ // Comparing different instances fails, even with same contents
906+ assert.deepStrictEqual(weakSet1, weakSet2);
889907// AssertionError: Expected inputs to be strictly deep-equal:
890908// + actual - expected
891909//
892- // WeakMap {
893- // + [items unknown]
894- // - [items unknown],
895- // - unequal: true
896- // }
910+ // + WeakSet { <items unknown> }
911+ // - WeakSet { <items unknown> }
912+
913+ // Comparing the same instance to itself succeeds
914+ assert.deepStrictEqual(weakSet1, weakSet1);
915+ // OK
897916```
898917
899918```cjs
@@ -969,23 +988,40 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
969988// }
970989
971990const weakMap1 = new WeakMap();
972- const weakMap2 = new WeakMap([[{}, {}]]);
973- const weakMap3 = new WeakMap();
974- weakMap3.unequal = true;
991+ const weakMap2 = new WeakMap();
992+ const obj = {};
975993
994+ weakMap1.set(obj, 'value');
995+ weakMap2.set(obj, 'value');
996+
997+ // Comparing different instances fails, even with same contents
976998assert.deepStrictEqual(weakMap1, weakMap2);
977- // OK, because it is impossible to compare the entries
999+ // AssertionError: Values have same structure but are not reference-equal:
1000+ //
1001+ // WeakMap {
1002+ // <items unknown>
1003+ // }
1004+
1005+ // Comparing the same instance to itself succeeds
1006+ assert.deepStrictEqual(weakMap1, weakMap1);
1007+ // OK
9781008
979- // Fails because weakMap3 has a property that weakMap1 does not contain:
980- assert.deepStrictEqual(weakMap1, weakMap3);
1009+ const weakSet1 = new WeakSet();
1010+ const weakSet2 = new WeakSet();
1011+ weakSet1.add(obj);
1012+ weakSet2.add(obj);
1013+
1014+ // Comparing different instances fails, even with same contents
1015+ assert.deepStrictEqual(weakSet1, weakSet2);
9811016// AssertionError: Expected inputs to be strictly deep-equal:
9821017// + actual - expected
9831018//
984- // WeakMap {
985- // + [items unknown]
986- // - [items unknown],
987- // - unequal: true
988- // }
1019+ // + WeakSet { <items unknown> }
1020+ // - WeakSet { <items unknown> }
1021+
1022+ // Comparing the same instance to itself succeeds
1023+ assert.deepStrictEqual(weakSet1, weakSet1);
1024+ // OK
9891025```
9901026
9911027If the values are not equal, an [`AssertionError`][] is thrown with a `message`
0 commit comments