Skip to content

Commit 936eea5

Browse files
committed
doc: document asserts Weak(Map|Set) behavior
Right now it is not documentated that WeakMap entries are not compared. This might result in some confusion. This adds a note about the behavior in `assert.deepStrictEqual`. This documentation is also references in `util.isDeepStrictEqual`, so we do not have to document it again for that function as the underlying algorithm is the same. PR-URL: #18248 Fixes: #18228 Refs: #18228 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent edbcf7c commit 936eea5

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

doc/api/assert.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ are recursively evaluated also by the following rules.
249249
* Map keys and Set items are compared unordered.
250250
* Recursion stops when both sides differ or both sides encounter a circular
251251
reference.
252+
* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values. See
253+
below for further details.
252254

253255
```js
254256
const assert = require('assert').strict;
@@ -290,6 +292,16 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 });
290292
// OK, because it is the same symbol on both objects.
291293
assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
292294
// Fails because symbol1 !== symbol2!
295+
296+
const weakMap1 = new WeakMap();
297+
const weakMap2 = new WeakMap([[{}, {}]]);
298+
const weakMap3 = new WeakMap();
299+
weakMap3.unequal = true;
300+
301+
assert.deepStrictEqual(weakMap1, weakMap2);
302+
// OK, because it is impossible to compare the entries
303+
assert.deepStrictEqual(weakMap1, weakMap3);
304+
// Fails because weakMap3 has a property that weakMap1 does not contain!
293305
```
294306

295307
If the values are not equal, an `AssertionError` is thrown with a `message`
@@ -920,6 +932,8 @@ second argument. This might lead to difficult-to-spot errors.
920932
[`Set`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Set
921933
[`Symbol`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Symbol
922934
[`TypeError`]: errors.html#errors_class_typeerror
935+
[`WeakMap`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/WeakMap
936+
[`WeakSet`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/WeakSet
923937
[`assert.deepEqual()`]: #assert_assert_deepequal_actual_expected_message
924938
[`assert.deepStrictEqual()`]: #assert_assert_deepstrictequal_actual_expected_message
925939
[`assert.notDeepStrictEqual()`]: #assert_assert_notdeepstrictequal_actual_expected_message

0 commit comments

Comments
 (0)