From 1d6f74eab148440829792c69c93aa817a8ef59d0 Mon Sep 17 00:00:00 2001 From: Devin Riegle Date: Tue, 24 Nov 2020 11:04:16 -0500 Subject: [PATCH] Add mockFn.mock.lastCall to easily retrieve the last call arguments --- docs/MockFunctionAPI.md | 13 +++++++++ docs/MockFunctions.md | 3 +++ .../jest-mock/src/__tests__/index.test.ts | 27 +++++++++++++++++++ packages/jest-mock/src/index.ts | 8 ++++++ 4 files changed, 51 insertions(+) diff --git a/docs/MockFunctionAPI.md b/docs/MockFunctionAPI.md index 5bb9002d4069..3b6454e75e84 100644 --- a/docs/MockFunctionAPI.md +++ b/docs/MockFunctionAPI.md @@ -77,6 +77,19 @@ mockFn.mock.instances[0] === a; // true mockFn.mock.instances[1] === b; // true ``` +### `mockFn.mock.lastCall` + +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`. + +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: + +```js +[ + 'arg3', + 'arg4', +]; +``` + ### `mockFn.mockClear()` Resets all information stored in the [`mockFn.mock.calls`](#mockfnmockcalls) and [`mockFn.mock.instances`](#mockfnmockinstances) arrays. diff --git a/docs/MockFunctions.md b/docs/MockFunctions.md index e28ba0960c37..3217a733a7e3 100644 --- a/docs/MockFunctions.md +++ b/docs/MockFunctions.md @@ -75,6 +75,9 @@ expect(someMockFunction.mock.instances.length).toBe(2); // The object returned by the first instantiation of this function // had a `name` property whose value was set to 'test' expect(someMockFunction.mock.instances[0].name).toEqual('test'); + +// The first argument of the last call to the function was 'test' +expect(someMockFunction.mock.lastCall[0]).toBe('test'); ``` ## Mock Return Values diff --git a/packages/jest-mock/src/__tests__/index.test.ts b/packages/jest-mock/src/__tests__/index.test.ts index d68857294cb4..0be108b2610b 100644 --- a/packages/jest-mock/src/__tests__/index.test.ts +++ b/packages/jest-mock/src/__tests__/index.test.ts @@ -1064,6 +1064,33 @@ describe('moduleMocker', () => { expect(fn.getMockName()).toBe('myMockFn'); }); + + test('jest.fn should provide the correct lastCall', () => { + const mock = jest.fn(); + + expect(mock.mock.lastCall).toBeUndefined(); + + mock('first'); + mock('second'); + mock('last', 'call'); + + expect(mock).toHaveBeenLastCalledWith('last', 'call'); + expect(mock.mock.lastCall).toEqual(['last', 'call']); + }); + + test('lastCall gets reset by mockReset', () => { + const mock = jest.fn(); + + mock('first'); + mock('last', 'call'); + + expect(mock.mock.lastCall).toEqual(['last', 'call']); + + mock.mockReset(); + + expect(mock.mock.lastCall).toBeUndefined(); + }); + test('mockName gets reset by mockReset', () => { const fn = jest.fn(); expect(fn.getMockName()).toBe('jest.fn()'); diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 3d3dcd50e199..aeab76303b6a 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -101,6 +101,10 @@ type MockFunctionState> = { calls: Array; instances: Array; invocationCallOrder: Array; + /** + * Getter for retrieving the last call arguments + */ + lastCall?: Y; /** * List of results of calls to the mock function. */ @@ -456,6 +460,9 @@ class ModuleMockerClass { state = this._defaultMockState(); this._mockState.set(f, state); } + + state.lastCall = state.calls[state.calls.length - 1]; + return state; } @@ -473,6 +480,7 @@ class ModuleMockerClass { Y > { return { + lastCall: undefined, calls: [], instances: [], invocationCallOrder: [],