Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 36d5959

Browse files
committed
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).
1 parent beb2f29 commit 36d5959

3 files changed

Lines changed: 9 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
### Features
44

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))
77

88
## 30.4.2
99

docs/MockFunctionAPI.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,10 @@ fn(42); // 'numeric'
567567
fn({user: 'alice'}); // 'user'
568568
```
569569

570-
Argument matching uses the same equality as [`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.
571571

572572
- **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** — objects and arrays are compared structurally.
573+
- **Deep equality** — objects, arrays, Maps, and Sets are compared structurally.
574574

575575
#### Multiple matchers
576576

@@ -588,7 +588,7 @@ fn('c'); // 3
588588
fn('z'); // undefined
589589
```
590590

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.
592592

593593
#### Onces and persistents
594594

@@ -650,7 +650,7 @@ fn('y'); // 'default' (no matcher fires)
650650

651651
#### Precedence
652652

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:
654654

655655
| Priority | Source |
656656
| --- | --- |
@@ -659,7 +659,7 @@ When multiple registrations could match a single call, the dispatcher resolves t
659659
| 3 | Last-registered matching branch with a persistent impl |
660660
| 4 | Base impl set on the parent (`fn.mockReturnValue`, the `spyOn` original method, etc.) — or `undefined` if none |
661661

662-
#### Composition
662+
#### On spies
663663

664664
Works with [`jest.spyOn()`](JestObjectAPI.md#jestspyonobject-methodname). Non-matching calls fall through to the spied object's original method:
665665

@@ -672,14 +672,7 @@ target.greet('world'); // 'hi world'
672672
target.greet('jest'); // 'hello jest' (original method)
673673
```
674674

675-
Works with `new` (constructor mocks). `new fn(args)` matches the same way as `fn(args)`:
676-
677-
```js
678-
const Service = jest.fn();
679-
Service.whenCalledWith('cfg').mockImplementation(() => ({connected: true}));
680-
681-
const s = new Service('cfg'); // {connected: true}
682-
```
675+
#### Resetting
683676

684677
`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.
685678

packages/jest-mock/src/__tests__/whenCalledWith.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ describe('whenCalledWith', () => {
505505
expect(made).toBe(sentinel);
506506
});
507507

508-
it('records constructor calls on the sub-mock`s mock.instances', () => {
508+
it("records constructor calls on the sub-mock's mock.instances", () => {
509509
const Ctor = moduleMocker.fn<(arg: string) => {kind: string}>();
510510
const branch = Ctor.whenCalledWith('A').mockImplementation(() => ({
511511
kind: 'made',

0 commit comments

Comments
 (0)