Skip to content

Commit b2db6cf

Browse files
authored
feat(jest-mock): add mockFn.mock.lastCall to retrieve last argument (#12285)
1 parent 8a62d98 commit b2db6cf

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

CHANGELOG.md

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

33
### Features
44

5+
- `[jest-mock]` Added `mockFn.mock.lastCall` to retrieve last argument ([#12285](https://github.com/facebook/jest/pull/12285))
6+
57
### Fixes
68

79
- `[expect]` Add a fix for `.toHaveProperty('')` ([#12251](https://github.com/facebook/jest/pull/12251))

docs/MockFunctionAPI.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ mockFn.mock.instances[0] === a; // true
7979
mockFn.mock.instances[1] === b; // true
8080
```
8181

82+
### `mockFn.mock.lastCall`
83+
84+
An array containing the call arguments of the last call that was made to this mock function. If the function was not called, it will return `undefined`.
85+
86+
For example: A mock function `f` that has been called twice, with the arguments `f('arg1', 'arg2')`, and then with the arguments `f('arg3', 'arg4')`, would have a `mock.lastCall` array that looks like this:
87+
88+
```js
89+
['arg3', 'arg4'];
90+
```
91+
8292
### `mockFn.mockClear()`
8393

8494
Clears all information stored in the [`mockFn.mock.calls`](#mockfnmockcalls), [`mockFn.mock.instances`](#mockfnmockinstances) and [`mockFn.mock.results`](#mockfnmockresults) arrays. Often this is useful when you want to clean up a mocks usage data between two assertions.

docs/MockFunctions.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ expect(someMockFunction.mock.instances.length).toBe(2);
7575
// The object returned by the first instantiation of this function
7676
// had a `name` property whose value was set to 'test'
7777
expect(someMockFunction.mock.instances[0].name).toEqual('test');
78+
79+
// The first argument of the last call to the function was 'test'
80+
expect(someMockFunction.mock.lastCall[0]).toBe('test');
7881
```
7982

8083
## Mock Return Values

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,32 @@ describe('moduleMocker', () => {
10641064
expect(fn.getMockName()).toBe('myMockFn');
10651065
});
10661066

1067+
test('jest.fn should provide the correct lastCall', () => {
1068+
const mock = jest.fn();
1069+
1070+
expect(mock.mock).not.toHaveProperty('lastCall');
1071+
1072+
mock('first');
1073+
mock('second');
1074+
mock('last', 'call');
1075+
1076+
expect(mock).toHaveBeenLastCalledWith('last', 'call');
1077+
expect(mock.mock.lastCall).toEqual(['last', 'call']);
1078+
});
1079+
1080+
test('lastCall gets reset by mockReset', () => {
1081+
const mock = jest.fn();
1082+
1083+
mock('first');
1084+
mock('last', 'call');
1085+
1086+
expect(mock.mock.lastCall).toEqual(['last', 'call']);
1087+
1088+
mock.mockReset();
1089+
1090+
expect(mock.mock).not.toHaveProperty('lastCall');
1091+
});
1092+
10671093
test('mockName gets reset by mockReset', () => {
10681094
const fn = jest.fn();
10691095
expect(fn.getMockName()).toBe('jest.fn()');

packages/jest-mock/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ type MockFunctionState<T, Y extends Array<unknown>> = {
166166
calls: Array<Y>;
167167
instances: Array<T>;
168168
invocationCallOrder: Array<number>;
169+
/**
170+
* Getter for retrieving the last call arguments
171+
*/
172+
lastCall?: Y;
169173
/**
170174
* List of results of calls to the mock function.
171175
*/
@@ -516,6 +520,9 @@ export class ModuleMocker {
516520
state = this._defaultMockState();
517521
this._mockState.set(f, state);
518522
}
523+
if (state.calls.length > 0) {
524+
state.lastCall = state.calls[state.calls.length - 1];
525+
}
519526
return state;
520527
}
521528

0 commit comments

Comments
 (0)