Skip to content

Commit bb21f0b

Browse files
gricardcpojer
authored andcommitted
Add restoreMocks config to fix #3580 (#5327)
* Added restoreMocks config option * Changed tests to use jest.spyOn, since that's what restore impacts * Changed tests to use correct toHaveBeenCalled() syntax * Added changelog entry
1 parent 7743c09 commit bb21f0b

File tree

22 files changed

+161
-0
lines changed

22 files changed

+161
-0
lines changed

CHANGELOG.md

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

55
* `[jest-cli]` Fix `EISDIR` when a directory is passed as an argument to `jest`.
66
([#5317](https://github.com/facebook/jest/pull/5317))
7+
* `[jest-config]` Added restoreMocks config option.
8+
([#5327](https://github.com/facebook/jest/pull/5327))
79

810
## jest 22.1.0
911

docs/Configuration.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,15 @@ argument:
605605
The function should either return a path to the module that should be resolved
606606
or throw an error if the module can't be found.
607607

608+
### `restoreMocks` [boolean]
609+
610+
Default: `false`
611+
612+
Automatically restore mock state between every test. Equivalent to calling
613+
`jest.restoreAllMocks()` between each test. This will lead to any mocks having
614+
their fake implementations removed and restores their initial
615+
implementation.
616+
608617
### `rootDir` [string]
609618

610619
Default: The root of the directory containing your jest's [config file](#) _or_

docs/MockFunctionAPI.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ Beware that `mockFn.mockRestore` only works when mock was created with
9696
`jest.spyOn`. Thus you have to take care of restoration yourself when manually
9797
assigning `jest.fn()`.
9898

99+
The [`restoreMocks`](configuration.html#restoremocks-boolean) configuration
100+
option is available to restore mocks automatically between tests.
101+
99102
### `mockFn.mockImplementation(fn)`
100103

101104
Accepts a function that should be used as the implementation of the mock. The

integration-tests/__tests__/__snapshots__/show_config.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
3232
\\"name\\": \\"[md5 hash]\\",
3333
\\"resetMocks\\": false,
3434
\\"resetModules\\": false,
35+
\\"restoreMocks\\": false,
3536
\\"rootDir\\": \\"<<REPLACED_ROOT_DIR>>\\",
3637
\\"roots\\": [
3738
\\"<<REPLACED_ROOT_DIR>>\\"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
'use strict';
10+
11+
const runJest = require('../runJest');
12+
13+
test('suite with auto-restore', () => {
14+
const result = runJest('auto-restore-mocks/with-auto-restore');
15+
expect(result.status).toBe(0);
16+
});
17+
18+
test('suite without auto-restore', () => {
19+
const result = runJest('auto-restore-mocks/without-auto-restore');
20+
expect(result.status).toBe(0);
21+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
const TestClass = require('../');
11+
const localClass = new TestClass();
12+
13+
test('first test', () => {
14+
jest.spyOn(localClass, 'test').mockImplementation(() => 'ABCD');
15+
expect(localClass.test()).toEqual('ABCD');
16+
expect(localClass.test).toHaveBeenCalledTimes(1);
17+
});
18+
19+
test('second test', () => {
20+
expect(localClass.test()).toEqual('12345');
21+
expect(localClass.test.mock).toBe(undefined);
22+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
module.exports = class Test {
9+
test() {
10+
return '12345';
11+
}
12+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node",
4+
"restoreMocks": true
5+
}
6+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
const TestClass = require('../');
11+
const localClass = new TestClass();
12+
13+
describe('without an explicit restore', () => {
14+
jest.spyOn(localClass, 'test').mockImplementation(() => 'ABCD');
15+
16+
test('first test', () => {
17+
expect(localClass.test()).toEqual('ABCD');
18+
expect(localClass.test).toHaveBeenCalledTimes(1);
19+
});
20+
21+
test('second test', () => {
22+
expect(localClass.test()).toEqual('ABCD');
23+
expect(localClass.test).toHaveBeenCalledTimes(2);
24+
});
25+
});
26+
27+
describe('with an explicit restore', () => {
28+
beforeEach(() => {
29+
jest.restoreAllMocks();
30+
});
31+
32+
test('first test', () => {
33+
jest.spyOn(localClass, 'test').mockImplementation(() => 'ABCD');
34+
expect(localClass.test()).toEqual('ABCD');
35+
expect(localClass.test).toHaveBeenCalledTimes(1);
36+
});
37+
38+
test('second test', () => {
39+
expect(localClass.test()).toEqual('12345');
40+
expect(localClass.test.mock).toBe(undefined);
41+
});
42+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
module.exports = class Test {
9+
test() {
10+
return '12345';
11+
}
12+
};

0 commit comments

Comments
 (0)