You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs(jest-mock): drop "dispatcher" word, split spies/resetting, clarify equality
Three rounds of nits from jeysal + Copilot:
- Drop the internal "dispatcher" word from user-facing prose (jeysal).
Replace with behavioral phrasing.
- Split "Composition" into "On spies" and "Resetting" subsections, and
drop the constructor paragraph — not specific to whenCalledWith
(jeysal).
- Rewrite the argument-matching paragraph to be honest about what we
actually use (`equals` + `iterableEquality`) and that user-extended
custom testers from `expect.extend(...)` don't apply (Copilot).
- Fix `sub-mock`s` backtick typo in the constructor test name (Copilot).
- Add PR link to changelog entries (Copilot).
Copy file name to clipboardExpand all lines: CHANGELOG.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,8 @@
2
2
3
3
### Features
4
4
5
-
-`[@jest/expect-utils, jest-mock]` Add `mockFn.whenCalledWith(...args)` for configuring return values per argument list, with first-class asymmetric-matcher support
6
-
-`[@jest/expect-utils]` Export `AsymmetricMatcher` type (previously private to `expect`)
5
+
-`[@jest/expect-utils, jest-mock]` Add `mockFn.whenCalledWith(...args)` for configuring return values per argument list, with first-class asymmetric-matcher support ([#16053](https://github.com/jestjs/jest/pull/16053))
6
+
-`[@jest/expect-utils]` Export `AsymmetricMatcher` type (previously private to `expect`) ([#16053](https://github.com/jestjs/jest/pull/16053))
Copy file name to clipboardExpand all lines: docs/MockFunctionAPI.md
+6-13Lines changed: 6 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -567,10 +567,10 @@ fn(42); // 'numeric'
567
567
fn({user:'alice'}); // 'user'
568
568
```
569
569
570
-
Argument matching uses the same equalityas [`toHaveBeenCalledWith()`](ExpectAPI.md#tohavebeencalledwitharg1-arg2-):
570
+
Argument matching uses `equals` from `@jest/expect-utils` with `iterableEquality`, so structural equality, Map/Set discrimination, and asymmetric matchers all behave as you'd expect from [`toHaveBeenCalledWith()`](ExpectAPI.md#tohavebeencalledwitharg1-arg2-). Custom testers registered via `expect.extend(...)` are **not** applied to `whenCalledWith` matching.
571
571
572
572
-**Arity** — `whenCalledWith('a')` matches `fn('a')` and `fn('a', undefined)`, but **not**`fn('a', 'b')` or `fn()`. (Trailing `undefined` is allowed for parity with Jest's existing call-tracking semantics.)
573
-
-**Deep equality** — objectsand arrays are compared structurally.
573
+
-**Deep equality** — objects, arrays, Maps, and Sets are compared structurally.
574
574
575
575
#### Multiple matchers
576
576
@@ -588,7 +588,7 @@ fn('c'); // 3
588
588
fn('z'); // undefined
589
589
```
590
590
591
-
Two registrations whose matchers _overlap_ on a given call (e.g. `expect.objectContaining({a: 1})` and `expect.objectContaining({b: 2})` against `fn({a: 1, b: 2})`) both match. The dispatcher resolves them via the precedence rules below.
591
+
Two registrations whose matchers _overlap_ on a given call (e.g. `expect.objectContaining({a: 1})` and `expect.objectContaining({b: 2})` against `fn({a: 1, b: 2})`) both match. The [precedence rules](#precedence) below decide which one fires.
592
592
593
593
#### Onces and persistents
594
594
@@ -650,7 +650,7 @@ fn('y'); // 'default' (no matcher fires)
650
650
651
651
#### Precedence
652
652
653
-
When multiple registrations could match a single call, the dispatcher resolves them in this order:
653
+
When multiple registrations could match a single call, they're resolved in this order:
654
654
655
655
| Priority | Source |
656
656
| --- | --- |
@@ -659,7 +659,7 @@ When multiple registrations could match a single call, the dispatcher resolves t
659
659
| 3 | Last-registered matching branch with a persistent impl |
660
660
| 4 | Base impl set on the parent (`fn.mockReturnValue`, the `spyOn` original method, etc.) — or `undefined` if none |
661
661
662
-
#### Composition
662
+
#### On spies
663
663
664
664
Works with [`jest.spyOn()`](JestObjectAPI.md#jestspyonobject-methodname). Non-matching calls fall through to the spied object's original method:
`mockReset()` on the parent clears all `whenCalledWith` registrations and cascades the reset to each sub-mock — so any references you kept reflect the reset state. Calling `mockReset()` on a sub-mock clears only that branch.
0 commit comments