Skip to content

Commit 968b867

Browse files
BridgeARMylesBorins
authored andcommitted
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. Backport-PR-URL: #19230 PR-URL: #18248 Fixes: #18228 Refs: #18228 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent e9ac468 commit 968b867

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

doc/api/assert.md

+14
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ are recursively evaluated also by the following rules.
251251
* Map keys and Set items are compared unordered.
252252
* Recursion stops when both sides differ or both sides encounter a circular
253253
reference.
254+
* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values. See
255+
below for further details.
254256

255257
```js
256258
const assert = require('assert').strict;
@@ -292,6 +294,16 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 });
292294
// OK, because it is the same symbol on both objects.
293295
assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
294296
// Fails because symbol1 !== symbol2!
297+
298+
const weakMap1 = new WeakMap();
299+
const weakMap2 = new WeakMap([[{}, {}]]);
300+
const weakMap3 = new WeakMap();
301+
weakMap3.unequal = true;
302+
303+
assert.deepStrictEqual(weakMap1, weakMap2);
304+
// OK, because it is impossible to compare the entries
305+
assert.deepStrictEqual(weakMap1, weakMap3);
306+
// Fails because weakMap3 has a property that weakMap1 does not contain!
295307
```
296308

297309
If the values are not equal, an `AssertionError` is thrown with a `message`
@@ -893,6 +905,8 @@ For more information, see
893905
[`Set`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
894906
[`Symbol`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol
895907
[`TypeError`]: errors.html#errors_class_typeerror
908+
[`WeakMap`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/WeakMap
909+
[`WeakSet`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/WeakSet
896910
[`assert.deepEqual()`]: #assert_assert_deepequal_actual_expected_message
897911
[`assert.deepStrictEqual()`]: #assert_assert_deepstrictequal_actual_expected_message
898912
[`assert.notDeepStrictEqual()`]: #assert_assert_notdeepstrictequal_actual_expected_message

0 commit comments

Comments
 (0)