Skip to content

Commit

Permalink
Improve the documentation of mockClear, mockReset, and `mockResto…
Browse files Browse the repository at this point in the history
…re` (especially surrounding `spyOn` behaviour)
  • Loading branch information
Stephen Cook committed May 29, 2018
1 parent 74892f0 commit 5cc2a0d
Show file tree
Hide file tree
Showing 7 changed files with 361 additions and 34 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@

### Chore & Maintenance

* `[docs]` Improve documentation of `mockClear`, `mockReset`, and `mockRestore`
([#6227](https://github.com/facebook/jest/pull/6227/files))
* `[jest-cli]` Use yargs's built-in `version` instead of rolling our own
([#6215](https://github.com/facebook/jest/pull/6215))
* `[docs]` Add explanation on how to mock methods not implemented in JSDOM
Expand Down
17 changes: 8 additions & 9 deletions docs/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,23 +301,25 @@ Returns the `jest` object for chaining.
### `jest.clearAllMocks()`

Clears the `mock.calls` and `mock.instances` properties of all mocks. Equivalent
to calling `.mockClear()` on every mocked function.
to calling [`.mockClear()`](MockFunctionAPI.md#mockfnmockclear) on every mocked
function.

Returns the `jest` object for chaining.

### `jest.resetAllMocks()`

Resets the state of all mocks. Equivalent to calling `.mockReset()` on every
mocked function.
Resets the state of all mocks. Equivalent to calling
[`.mockReset()`](MockFunctionAPI.md#mockfnmockreset) on every mocked function.

Returns the `jest` object for chaining.

### `jest.restoreAllMocks()`

Restores all mocks back to their original value. Equivalent to calling
`.mockRestore` on every mocked function. Beware that `jest.restoreAllMocks()`
only works when mock was created with `jest.spyOn`; other mocks will require you
to manually restore them.
`.mockRestore` on every mocked function. Beware that
[`jest.restoreAllMocks()`](MockFunctionAPI.md#mockfnmockrestore) only works when
mock was created with `jest.spyOn`; other mocks will require you to manually
restore them.

### `jest.resetModules()`

Expand Down Expand Up @@ -495,7 +497,6 @@ test('plays video', () => {
expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);

spy.mockReset();
spy.mockRestore();
});
```
Expand Down Expand Up @@ -544,7 +545,6 @@ test('plays video', () => {
expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);

spy.mockReset();
spy.mockRestore();
});

Expand All @@ -557,7 +557,6 @@ test('plays audio', () => {
expect(spy).toHaveBeenCalled();
expect(audio.volume).toBe(100);

spy.mockReset();
spy.mockRestore();
});
```
12 changes: 7 additions & 5 deletions docs/MockFunctionAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ is available to clear mocks automatically between tests.

### `mockFn.mockReset()`

Resets all information stored in the mock, including any initial implementation
and mock name given.
Does everything that [`mockFn.mockClear()`](#mockfnmockclear) does, and also
removes any mocked return values or implementations.

This is useful when you want to completely restore a mock back to its initial
state.
This is useful when you want to completely reset a _mock_ back to its initial
state. (Note that resetting a _spy_ will result in a function with no return
value).

Beware that `mockReset` will replace `mockFn.mock`, not just
[`mockFn.mock.calls`](#mockfn-mock-calls) and
Expand All @@ -116,7 +117,8 @@ don't access stale data.

### `mockFn.mockRestore()`

Removes the mock and restores the initial implementation.
Does everything that [`mockFn.mockReset()`](#mockfnmockreset) does, and also
restores the original (non-mocked) implementation.

This is useful when you want to mock functions in certain test cases and restore
the original implementation in others.
Expand Down
12 changes: 4 additions & 8 deletions packages/jest-mock/src/__tests__/jest_mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,12 +737,13 @@ describe('moduleMocker', () => {
expect(fn.getMockName()).toBe('jest.fn()');
});

test('mockName is not reset by mockRestore', () => {
const fn = jest.fn(() => false);
test('mockName gets reset by mockRestore', () => {
const fn = jest.fn();
expect(fn.getMockName()).toBe('jest.fn()');
fn.mockName('myMockFn');
expect(fn.getMockName()).toBe('myMockFn');
fn.mockRestore();
expect(fn.getMockName()).toBe('myMockFn');
expect(fn.getMockName()).toBe('jest.fn()');
});

test('mockName is not reset by mockClear', () => {
Expand Down Expand Up @@ -782,7 +783,6 @@ describe('moduleMocker', () => {
isOriginalCalled = false;
originalCallThis = null;
originalCallArguments = null;
spy.mockReset();
spy.mockRestore();
obj.method.call(thisArg, firstArg, secondArg);
expect(isOriginalCalled).toBe(true);
Expand Down Expand Up @@ -873,7 +873,6 @@ describe('moduleMocker', () => {
isOriginalCalled = false;
originalCallThis = null;
originalCallArguments = null;
spy.mockReset();
spy.mockRestore();
obj.method.call(thisArg, firstArg, secondArg);
expect(isOriginalCalled).toBe(true);
Expand All @@ -900,7 +899,6 @@ describe('moduleMocker', () => {
expect(spy).toHaveBeenCalled();
expect(obj.property).toBe(true);
obj.property = false;
spy.mockReset();
spy.mockRestore();
obj.property = true;
expect(spy).not.toHaveBeenCalled();
Expand Down Expand Up @@ -990,7 +988,6 @@ describe('moduleMocker', () => {
isOriginalCalled = false;
originalCallThis = null;
originalCallArguments = null;
spy.mockReset();
spy.mockRestore();
obj.method.call(thisArg, firstArg, secondArg);
expect(isOriginalCalled).toBe(true);
Expand Down Expand Up @@ -1018,7 +1015,6 @@ describe('moduleMocker', () => {
expect(spy).toHaveBeenCalled();
expect(obj.property).toBe(true);
obj.property = false;
spy.mockReset();
spy.mockRestore();
obj.property = true;
expect(spy).not.toHaveBeenCalled();
Expand Down
9 changes: 6 additions & 3 deletions packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,16 @@ class ModuleMockerClass {
};

f.mockReset = () => {
this._mockState.delete(f);
f.mockClear();
this._mockConfigRegistry.delete(f);
return f;
};

f.mockRestore = () => {
f.mockReset();
return restore ? restore() : undefined;
};

f.mockReturnValueOnce = value => {
// next function call will return this value or default return value
const mockConfig = this._ensureMockConfig(f);
Expand Down Expand Up @@ -528,8 +533,6 @@ class ModuleMockerClass {
f.mockImplementation(metadata.mockImpl);
}

f.mockRestore = restore ? restore : () => {};

return f;
} else {
const unknownType = metadata.type || 'undefined type';
Expand Down
17 changes: 8 additions & 9 deletions website/versioned_docs/version-22.4/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,23 +302,25 @@ Returns the `jest` object for chaining.
### `jest.clearAllMocks()`

Clears the `mock.calls` and `mock.instances` properties of all mocks. Equivalent
to calling `.mockClear()` on every mocked function.
to calling [`.mockClear()`](MockFunctionAPI.md#mockfnmockclear) on every mocked
function.

Returns the `jest` object for chaining.

### `jest.resetAllMocks()`

Resets the state of all mocks. Equivalent to calling `.mockReset()` on every
mocked function.
Resets the state of all mocks. Equivalent to calling
[`.mockReset()`](MockFunctionAPI.md#mockfnmockreset) on every mocked function.

Returns the `jest` object for chaining.

### `jest.restoreAllMocks()`

Restores all mocks back to their original value. Equivalent to calling
`.mockRestore` on every mocked function. Beware that `jest.restoreAllMocks()`
only works when mock was created with `jest.spyOn`; other mocks will require you
to manually restore them.
`.mockRestore` on every mocked function. Beware that
[`jest.restoreAllMocks()`](MockFunctionAPI.md#mockfnmockrestore) only works when
mock was created with `jest.spyOn`; other mocks will require you to manually
restore them.

### `jest.resetModules()`

Expand Down Expand Up @@ -496,7 +498,6 @@ test('plays video', () => {
expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);

spy.mockReset();
spy.mockRestore();
});
```
Expand Down Expand Up @@ -545,7 +546,6 @@ test('plays video', () => {
expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);

spy.mockReset();
spy.mockRestore();
});

Expand All @@ -556,7 +556,6 @@ test('plays audio', () => {
expect(spy).toHaveBeenCalled();
expect(video.volume).toBe(100);

spy.mockReset();
spy.mockRestore();
});
```
Loading

0 comments on commit 5cc2a0d

Please sign in to comment.