@@ -804,8 +804,10 @@ are recursively evaluated also by the following rules.
804
804
* [ ` Map ` ] [ ] keys and [ ` Set ` ] [ ] items are compared unordered.
805
805
* Recursion stops when both sides differ or both sides encounter a circular
806
806
reference.
807
- * [ ` WeakMap ` ] [ ] and [ ` WeakSet ` ] [ ] comparison does not rely on their values. See
808
- below for further details.
807
+ * [ ` WeakMap ` ] [ ] and [ ` WeakSet ` ] [ ] instances are ** not** compared structurally.
808
+ They are only equal if they reference the same object. Any comparison between
809
+ different ` WeakMap ` or ` WeakSet ` instances will result in inequality,
810
+ even if they contain the same entries.
809
811
* [ ` RegExp ` ] [ ] lastIndex, flags, and source are always compared, even if these
810
812
are not enumerable properties.
811
813
@@ -882,23 +884,40 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
882
884
// }
883
885
884
886
const weakMap1 = new WeakMap ();
885
- const weakMap2 = new WeakMap ([[{}, {}]]);
886
- const weakMap3 = new WeakMap ();
887
- weakMap3 .unequal = true ;
887
+ const weakMap2 = new WeakMap ();
888
+ const obj = {};
888
889
890
+ weakMap1 .set (obj, ' value' );
891
+ weakMap2 .set (obj, ' value' );
892
+
893
+ // Comparing different instances fails, even with same contents
889
894
assert .deepStrictEqual (weakMap1, weakMap2);
890
- // OK, because it is impossible to compare the entries
895
+ // AssertionError: Values have same structure but are not reference-equal:
896
+ //
897
+ // WeakMap {
898
+ // <items unknown>
899
+ // }
900
+
901
+ // Comparing the same instance to itself succeeds
902
+ assert .deepStrictEqual (weakMap1, weakMap1);
903
+ // OK
891
904
892
- // Fails because weakMap3 has a property that weakMap1 does not contain:
893
- assert .deepStrictEqual (weakMap1, weakMap3);
905
+ const weakSet1 = new WeakSet ();
906
+ const weakSet2 = new WeakSet ();
907
+ weakSet1 .add (obj);
908
+ weakSet2 .add (obj);
909
+
910
+ // Comparing different instances fails, even with same contents
911
+ assert .deepStrictEqual (weakSet1, weakSet2);
894
912
// AssertionError: Expected inputs to be strictly deep-equal:
895
913
// + actual - expected
896
914
//
897
- // WeakMap {
898
- // + [items unknown]
899
- // - [items unknown],
900
- // - unequal: true
901
- // }
915
+ // + WeakSet { <items unknown> }
916
+ // - WeakSet { <items unknown> }
917
+
918
+ // Comparing the same instance to itself succeeds
919
+ assert .deepStrictEqual (weakSet1, weakSet1);
920
+ // OK
902
921
```
903
922
904
923
``` cjs
@@ -974,23 +993,40 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
974
993
// }
975
994
976
995
const weakMap1 = new WeakMap ();
977
- const weakMap2 = new WeakMap ([[{}, {}]]);
978
- const weakMap3 = new WeakMap ();
979
- weakMap3 .unequal = true ;
996
+ const weakMap2 = new WeakMap ();
997
+ const obj = {};
980
998
999
+ weakMap1 .set (obj, ' value' );
1000
+ weakMap2 .set (obj, ' value' );
1001
+
1002
+ // Comparing different instances fails, even with same contents
981
1003
assert .deepStrictEqual (weakMap1, weakMap2);
982
- // OK, because it is impossible to compare the entries
1004
+ // AssertionError: Values have same structure but are not reference-equal:
1005
+ //
1006
+ // WeakMap {
1007
+ // <items unknown>
1008
+ // }
1009
+
1010
+ // Comparing the same instance to itself succeeds
1011
+ assert .deepStrictEqual (weakMap1, weakMap1);
1012
+ // OK
983
1013
984
- // Fails because weakMap3 has a property that weakMap1 does not contain:
985
- assert .deepStrictEqual (weakMap1, weakMap3);
1014
+ const weakSet1 = new WeakSet ();
1015
+ const weakSet2 = new WeakSet ();
1016
+ weakSet1 .add (obj);
1017
+ weakSet2 .add (obj);
1018
+
1019
+ // Comparing different instances fails, even with same contents
1020
+ assert .deepStrictEqual (weakSet1, weakSet2);
986
1021
// AssertionError: Expected inputs to be strictly deep-equal:
987
1022
// + actual - expected
988
1023
//
989
- // WeakMap {
990
- // + [items unknown]
991
- // - [items unknown],
992
- // - unequal: true
993
- // }
1024
+ // + WeakSet { <items unknown> }
1025
+ // - WeakSet { <items unknown> }
1026
+
1027
+ // Comparing the same instance to itself succeeds
1028
+ assert .deepStrictEqual (weakSet1, weakSet1);
1029
+ // OK
994
1030
```
995
1031
996
1032
If the values are not equal, an [ ` AssertionError ` ] [ ] is thrown with a ` message `
0 commit comments