@@ -799,8 +799,10 @@ are recursively evaluated also by the following rules.
|
799 | 799 | * [`Map`][] keys and [`Set`][] items are compared unordered.
|
800 | 800 | * Recursion stops when both sides differ or both sides encounter a circular
|
801 | 801 | 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. |
804 | 806 | * [`RegExp`][] lastIndex, flags, and source are always compared, even if these
|
805 | 807 | are not enumerable properties.
|
806 | 808 |
|
@@ -877,23 +879,40 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
|
877 | 879 | // }
|
878 | 880 |
|
879 | 881 | const 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 = {}; |
883 | 884 |
|
| 885 | +weakMap1.set(obj, 'value'); |
| 886 | +weakMap2.set(obj, 'value'); |
| 887 | + |
| 888 | +// Comparing different instances fails, even with same contents |
884 | 889 | assert.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 |
886 | 899 |
|
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); |
889 | 907 | // AssertionError: Expected inputs to be strictly deep-equal:
|
890 | 908 | // + actual - expected
|
891 | 909 | //
|
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 |
897 | 916 | ```
|
898 | 917 |
|
899 | 918 | ```cjs
|
@@ -969,23 +988,40 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
|
969 | 988 | // }
|
970 | 989 |
|
971 | 990 | const 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 = {}; |
975 | 993 |
|
| 994 | +weakMap1.set(obj, 'value'); |
| 995 | +weakMap2.set(obj, 'value'); |
| 996 | + |
| 997 | +// Comparing different instances fails, even with same contents |
976 | 998 | assert.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 |
978 | 1008 |
|
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); |
981 | 1016 | // AssertionError: Expected inputs to be strictly deep-equal:
|
982 | 1017 | // + actual - expected
|
983 | 1018 | //
|
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 |
989 | 1025 | ```
|
990 | 1026 |
|
991 | 1027 | If the values are not equal, an [`AssertionError`][] is thrown with a `message`
|
|
0 commit comments