Skip to content

Commit

Permalink
Update mockReturnValue documentation (#3993)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbay authored and cpojer committed Jul 10, 2017
1 parent 56b24c5 commit 487ab1d
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions docs/en/MockFunctionAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Often this is useful when you want to clean up a mock's usage data between two a

Beware that `mockClear` will replace `mockFn.mock`, not just [`mockFn.mock.calls`](#mockfn-mock-calls) and [`mockFn.mock.instances`](#mockfn-mock-instances). You should therefore avoid assigning `mockFn.mock` to other variables, temporary or not, to make sure you don't access stale data.

The [`clearMocks`](configuration.html#clearmocks-boolean) configuration option is available to clear mocks automatically between tests.

### `mockFn.mockReset()`
Resets all information stored in the mock, including any inital implementation given.

Expand All @@ -69,7 +71,7 @@ Beware that `mockFn.mockRestore` only works when mock was created with `jest.spy
### `mockFn.mockImplementation(fn)`
Accepts a function that should be used as the implementation of the mock. The mock itself will still record all calls that go into and instances that come from itself – the only difference is that the implementation will also be executed when the mock is called.

*Note: `jest.fn(implementation)` is a shorthand for `mockImplementation`.*
*Note: `jest.fn(implementation)` is a shorthand for `jest.fn().mockImplementation(implementation)`.*

For example:

Expand Down Expand Up @@ -145,26 +147,25 @@ jest.fn(function() {
```

### `mockFn.mockReturnValue(value)`

Deprecated: Use `jest.fn(() => value)` instead.
Accepts a value that will be returned whenever the mock function is called.

```js
const mockNumberFn = jest.fn(() => 42);
mockNumberFn(); // 42

// Deprecated behavior:
jest.genMockFunction().mockImplementation(() => value);
const mock = jest.fn();
mock.mockReturnValue(42);
mock(); // 42
mock.mockReturnValue(43);
mock(); // 43
```

### `mockFn.mockReturnValueOnce(value)`
Just a simple sugar function for:
Accepts a value that will be returned for one call to the mock function. Can be chained so that successive calls to the mock function return different values. When there are no more `mockReturnValueOnce` values to use, calls will return a value specified by `mockReturnValue`.

```js
let valueReturned = false;
jest.fn(() => {
if (!valueReturned) {
valueReturned = true;
return value;
}
});
```
const myMockFn = jest.fn()
.mockReturnValue('default')
.mockReturnValueOnce('first call')
.mockReturnValueOnce('second call');
console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn());
> 'first call', 'second call', 'default', 'default'
```

0 comments on commit 487ab1d

Please sign in to comment.