Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jest-mock): add mockFn.mock.lastCall to retrieve last argument #12285

Merged
merged 7 commits into from Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

### Features

- `[mockFunctions]` Added mockFn.mock.lastCall to retreive last Arguement ([#12285](https://github.com/facebook/jest/pull/12285))
SimenB marked this conversation as resolved.
Show resolved Hide resolved

### Fixes

- `[@jest/transform]` Update dependency package `pirates` to 4.0.4 ([#12136](https://github.com/facebook/jest/pull/12136))
Expand Down
10 changes: 10 additions & 0 deletions docs/MockFunctionAPI.md
Expand Up @@ -79,6 +79,16 @@ 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()`

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.
Expand Down
3 changes: 3 additions & 0 deletions docs/MockFunctions.md
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions packages/jest-mock/src/__tests__/index.test.ts
Expand Up @@ -1064,6 +1064,32 @@ describe('moduleMocker', () => {
expect(fn.getMockName()).toBe('myMockFn');
});

test('jest.fn should provide the correct lastCall', () => {
const mock = jest.fn();

expect(mock.mock.lastCall).toBeUndefined();
Biki-das marked this conversation as resolved.
Show resolved Hide resolved

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();
Biki-das marked this conversation as resolved.
Show resolved Hide resolved
});

test('mockName gets reset by mockReset', () => {
const fn = jest.fn();
expect(fn.getMockName()).toBe('jest.fn()');
Expand Down
8 changes: 8 additions & 0 deletions packages/jest-mock/src/index.ts
Expand Up @@ -166,6 +166,10 @@ type MockFunctionState<T, Y extends Array<unknown>> = {
calls: Array<Y>;
instances: Array<T>;
invocationCallOrder: Array<number>;
/**
* Getter for retrieving the last call arguments
*/
lastCall?: Y;
/**
* List of results of calls to the mock function.
*/
Expand Down Expand Up @@ -516,6 +520,9 @@ export class ModuleMocker {
state = this._defaultMockState();
this._mockState.set(f, state);
}

state.lastCall = state.calls[state.calls.length - 1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if state.calls.length > 0 first


return state;
}

Expand All @@ -536,6 +543,7 @@ export class ModuleMocker {
calls: [],
instances: [],
invocationCallOrder: [],
lastCall: undefined,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should just not be here I think, not set to undefined (which might be a return value)

results: [],
};
}
Expand Down
10 changes: 10 additions & 0 deletions website/versioned_docs/version-25.x/MockFunctionAPI.md
Expand Up @@ -79,6 +79,16 @@ mockFn.mock.instances[0] === a; // true
mockFn.mock.instances[1] === b; // true
```

### `mockFn.mock.lastCall`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should not be added to all versioned docs, only the ones in docs/


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()`

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.
Expand Down
3 changes: 3 additions & 0 deletions website/versioned_docs/version-25.x/MockFunctions.md
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions website/versioned_docs/version-26.x/MockFunctionAPI.md
Expand Up @@ -79,6 +79,16 @@ 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()`

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.
Expand Down
3 changes: 3 additions & 0 deletions website/versioned_docs/version-26.x/MockFunctions.md
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions website/versioned_docs/version-27.0/MockFunctionAPI.md
Expand Up @@ -79,6 +79,16 @@ 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()`

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.
Expand Down
3 changes: 3 additions & 0 deletions website/versioned_docs/version-27.0/MockFunctions.md
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions website/versioned_docs/version-27.1/MockFunctionAPI.md
Expand Up @@ -79,6 +79,16 @@ 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()`

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.
Expand Down
3 changes: 3 additions & 0 deletions website/versioned_docs/version-27.1/MockFunctions.md
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions website/versioned_docs/version-27.2/MockFunctionAPI.md
Expand Up @@ -79,6 +79,16 @@ 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()`

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.
Expand Down
3 changes: 3 additions & 0 deletions website/versioned_docs/version-27.2/MockFunctions.md
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions website/versioned_docs/version-27.4/MockFunctions.md
Expand Up @@ -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
Expand Down