Skip to content

Commit 2e6c217

Browse files
authored
fix(cli, config, docs): improve mock related cli messages, config template entries and documentation (#12047)
1 parent ee24dfc commit 2e6c217

22 files changed

Lines changed: 149 additions & 50 deletions

File tree

docs/CLI.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ When this option is provided, Jest will assume it is running in a CI environment
138138

139139
Deletes the Jest cache directory and then exits without running tests. Will delete `cacheDirectory` if the option is passed, or Jest's default cache directory. The default cache directory can be found by calling `jest --showConfig`. _Note: clearing the cache will reduce performance._
140140

141+
### `--clearMocks`
142+
143+
Automatically clear mock calls, instances and results before every test. Equivalent to calling [`jest.clearAllMocks()`](JestObjectAPI.md#jestclearallmocks) before each test. This does not remove any mock implementation that may have been provided.
144+
141145
### `--collectCoverageFrom=<glob>`
142146

143147
A glob pattern relative to `rootDir` matching the files that coverage info needs to be collected from.
@@ -272,6 +276,14 @@ Run tests with specified reporters. [Reporter options](configuration#reporters-a
272276

273277
`jest --reporters="default" --reporters="jest-junit"`
274278

279+
### `--resetMocks`
280+
281+
Automatically reset mock state before every test. Equivalent to calling [`jest.resetAllMocks()`](JestObjectAPI.md#jestresetallmocks) before each test. This will lead to any mocks having their fake implementations removed but does not restore their initial implementation.
282+
283+
### `--restoreMocks`
284+
285+
Automatically restore mock state and implementation before every test. Equivalent to calling [`jest.restoreAllMocks()`](JestObjectAPI.md#jestrestoreallmocks) before each test. This will lead to any mocks having their fake implementations removed and restores their initial implementation.
286+
275287
### `--roots`
276288

277289
A list of paths to directories that Jest should use to search for files in.

docs/Configuration.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ Jest attempts to scan your dependency tree once (up-front) and cache it in order
146146

147147
Default: `false`
148148

149-
Automatically clear mock calls and instances before every test. Equivalent to calling `jest.clearAllMocks()` before each test. This does not remove any mock implementation that may have been provided.
149+
Automatically clear mock calls, instances and results before every test. Equivalent to calling [`jest.clearAllMocks()`](JestObjectAPI.md#jestclearallmocks) before each test. This does not remove any mock implementation that may have been provided.
150150

151151
### `collectCoverage` \[boolean]
152152

@@ -764,7 +764,7 @@ For the full list of methods and argument types see `Reporter` interface in [pac
764764

765765
Default: `false`
766766

767-
Automatically reset mock state before every test. Equivalent to calling `jest.resetAllMocks()` before each test. This will lead to any mocks having their fake implementations removed but does not restore their initial implementation.
767+
Automatically reset mock state before every test. Equivalent to calling [`jest.resetAllMocks()`](JestObjectAPI.md#jestresetallmocks) before each test. This will lead to any mocks having their fake implementations removed but does not restore their initial implementation.
768768

769769
### `resetModules` \[boolean]
770770

@@ -849,7 +849,7 @@ While Jest does not support [package `exports`](https://nodejs.org/api/packages.
849849

850850
Default: `false`
851851

852-
Automatically restore mock state before every test. Equivalent to calling `jest.restoreAllMocks()` before each test. This will lead to any mocks having their fake implementations removed and restores their initial implementation.
852+
Automatically restore mock state and implementation before every test. Equivalent to calling [`jest.restoreAllMocks()`](JestObjectAPI.md#jestrestoreallmocks) before each test. This will lead to any mocks having their fake implementations removed and restores their initial implementation.
853853

854854
### `rootDir` \[string]
855855

docs/MockFunctionAPI.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,20 @@ mockFn.mock.instances[1] === b; // true
8181

8282
### `mockFn.mockClear()`
8383

84-
Resets 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.
84+
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.
8585

86-
Beware that `mockClear` will replace `mockFn.mock`, not just these three properties! 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#clearmocks-boolean) configuration option is available to clear mocks automatically between tests.
86+
Beware that `mockClear` will replace `mockFn.mock`, not just these three properties! You should, therefore, avoid assigning `mockFn.mock` to other variables, temporary or not, to make sure you don't access stale data.
87+
88+
The [`clearMocks`](configuration#clearmocks-boolean) configuration option is available to clear mocks automatically before each tests.
8789

8890
### `mockFn.mockReset()`
8991

9092
Does everything that [`mockFn.mockClear()`](#mockfnmockclear) does, and also removes any mocked return values or implementations.
9193

9294
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).
9395

96+
The [`mockReset`](configuration#resetmocks-boolean) configuration option is available to reset mocks automatically before each test.
97+
9498
### `mockFn.mockRestore()`
9599

96100
Does everything that [`mockFn.mockReset()`](#mockfnmockreset) does, and also restores the original (non-mocked) implementation.
@@ -99,7 +103,7 @@ This is useful when you want to mock functions in certain test cases and restore
99103

100104
Beware that `mockFn.mockRestore` only works when the mock was created with `jest.spyOn`. Thus you have to take care of restoration yourself when manually assigning `jest.fn()`.
101105

102-
The [`restoreMocks`](configuration#restoremocks-boolean) configuration option is available to restore mocks automatically between tests.
106+
The [`restoreMocks`](configuration#restoremocks-boolean) configuration option is available to restore mocks automatically before each test.
103107

104108
### `mockFn.mockImplementation(fn)`
105109

packages/jest-cli/src/cli/args.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ export const options = {
148148
},
149149
clearMocks: {
150150
description:
151-
'Automatically clear mock calls and instances between every ' +
152-
'test. Equivalent to calling jest.clearAllMocks() between each test.',
151+
'Automatically clear mock calls, instances and results before every test. ' +
152+
'Equivalent to calling jest.clearAllMocks() before each test.',
153153
type: 'boolean',
154154
},
155155
collectCoverage: {
@@ -440,8 +440,8 @@ export const options = {
440440
},
441441
resetMocks: {
442442
description:
443-
'Automatically reset mock state between every test. ' +
444-
'Equivalent to calling jest.resetAllMocks() between each test.',
443+
'Automatically reset mock state before every test. ' +
444+
'Equivalent to calling jest.resetAllMocks() before each test.',
445445
type: 'boolean',
446446
},
447447
resetModules: {
@@ -456,8 +456,8 @@ export const options = {
456456
},
457457
restoreMocks: {
458458
description:
459-
'Automatically restore mock state and implementation between every test. ' +
460-
'Equivalent to calling jest.restoreAllMocks() between each test.',
459+
'Automatically restore mock state and implementation before every test. ' +
460+
'Equivalent to calling jest.restoreAllMocks() before each test.',
461461
type: 'boolean',
462462
},
463463
rootDir: {

packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Array [
108108
},
109109
Object {
110110
"initial": false,
111-
"message": "Automatically clear mock calls and instances between every test?",
111+
"message": "Automatically clear mock calls, instances and results before every test?",
112112
"name": "clearMocks",
113113
"type": "confirm",
114114
},
@@ -131,7 +131,7 @@ module.exports = {
131131
// The directory where Jest should store its cached dependency information
132132
// cacheDirectory: \\"/tmp/jest\\",
133133
134-
// Automatically clear mock calls and instances between every test
134+
// Automatically clear mock calls, instances and results before every test
135135
// clearMocks: false,
136136
137137
// Indicates whether the coverage information should be collected while executing the test
@@ -219,7 +219,7 @@ module.exports = {
219219
// Use this configuration option to add custom reporters to Jest
220220
// reporters: undefined,
221221
222-
// Automatically reset mock state between every test
222+
// Automatically reset mock state before every test
223223
// resetMocks: false,
224224
225225
// Reset the module registry before running each individual test
@@ -228,7 +228,7 @@ module.exports = {
228228
// A path to a custom resolver
229229
// resolver: undefined,
230230
231-
// Automatically restore mock state between every test
231+
// Automatically restore mock state and implementation before every test
232232
// restoreMocks: false,
233233
234234
// The root directory that Jest should scan for tests and modules within

packages/jest-cli/src/init/questions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ const defaultQuestions: Array<PromptObject> = [
4242
},
4343
{
4444
initial: false,
45-
message: 'Automatically clear mock calls and instances between every test?',
45+
message:
46+
'Automatically clear mock calls, instances and results before every test?',
4647
name: 'clearMocks',
4748
type: 'confirm',
4849
},

packages/jest-config/src/Descriptions.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const descriptions: {[key in keyof Config.InitialOptions]: string} = {
1212
bail: 'Stop running tests after `n` failures',
1313
cacheDirectory:
1414
'The directory where Jest should store its cached dependency information',
15-
clearMocks: 'Automatically clear mock calls and instances between every test',
15+
clearMocks:
16+
'Automatically clear mock calls, instances and results before every test',
1617
collectCoverage:
1718
'Indicates whether the coverage information should be collected while executing the test',
1819
collectCoverageFrom:
@@ -53,10 +54,11 @@ const descriptions: {[key in keyof Config.InitialOptions]: string} = {
5354
preset: "A preset that is used as a base for Jest's configuration",
5455
projects: 'Run tests from one or more projects',
5556
reporters: 'Use this configuration option to add custom reporters to Jest',
56-
resetMocks: 'Automatically reset mock state between every test',
57+
resetMocks: 'Automatically reset mock state before every test',
5758
resetModules: 'Reset the module registry before running each individual test',
5859
resolver: 'A path to a custom resolver',
59-
restoreMocks: 'Automatically restore mock state between every test',
60+
restoreMocks:
61+
'Automatically restore mock state and implementation before every test',
6062
rootDir:
6163
'The root directory that Jest should scan for tests and modules within',
6264
roots:

website/versioned_docs/version-25.x/CLI.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ When this option is provided, Jest will assume it is running in a CI environment
138138

139139
Deletes the Jest cache directory and then exits without running tests. Will delete `cacheDirectory` if the option is passed, or Jest's default cache directory. The default cache directory can be found by calling `jest --showConfig`. _Note: clearing the cache will reduce performance._
140140

141+
### `--clearMocks`
142+
143+
Automatically clear mock calls, instances and results before every test. Equivalent to calling [`jest.clearAllMocks()`](JestObjectAPI.md#jestclearallmocks) before each test. This does not remove any mock implementation that may have been provided.
144+
141145
### `--collectCoverageFrom=<glob>`
142146

143147
A glob pattern relative to `rootDir` matching the files that coverage info needs to be collected from.
@@ -260,6 +264,14 @@ Run tests with specified reporters. [Reporter options](configuration#reporters-a
260264

261265
`jest --reporters="default" --reporters="jest-junit"`
262266

267+
### `--resetMocks`
268+
269+
Automatically reset mock state before every test. Equivalent to calling [`jest.resetAllMocks()`](JestObjectAPI.md#jestresetallmocks) before each test. This will lead to any mocks having their fake implementations removed but does not restore their initial implementation.
270+
271+
### `--restoreMocks`
272+
273+
Automatically restore mock state and implementation before every test. Equivalent to calling [`jest.restoreAllMocks()`](JestObjectAPI.md#jestrestoreallmocks) before each test. This will lead to any mocks having their fake implementations removed and restores their initial implementation.
274+
263275
### `--roots`
264276

265277
A list of paths to directories that Jest should use to search for files in.

website/versioned_docs/version-25.x/Configuration.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Jest attempts to scan your dependency tree once (up-front) and cache it in order
125125

126126
Default: `false`
127127

128-
Automatically clear mock calls and instances before every test. Equivalent to calling `jest.clearAllMocks()` before each test. This does not remove any mock implementation that may have been provided.
128+
Automatically clear mock calls, instances and results before every test. Equivalent to calling [`jest.clearAllMocks()`](JestObjectAPI.md#jestclearallmocks) before each test. This does not remove any mock implementation that may have been provided.
129129

130130
### `collectCoverage` \[boolean]
131131

@@ -702,7 +702,7 @@ For the full list of methods and argument types see `Reporter` interface in [pac
702702

703703
Default: `false`
704704

705-
Automatically reset mock state before every test. Equivalent to calling `jest.resetAllMocks()` before each test. This will lead to any mocks having their fake implementations removed but does not restore their initial implementation.
705+
Automatically reset mock state before every test. Equivalent to calling [`jest.resetAllMocks()`](JestObjectAPI.md#jestresetallmocks) before each test. This will lead to any mocks having their fake implementations removed but does not restore their initial implementation.
706706

707707
### `resetModules` \[boolean]
708708

@@ -736,7 +736,7 @@ Note: the defaultResolver passed as an option is the Jest default resolver which
736736

737737
Default: `false`
738738

739-
Automatically restore mock state before every test. Equivalent to calling `jest.restoreAllMocks()` before each test. This will lead to any mocks having their fake implementations removed and restores their initial implementation.
739+
Automatically restore mock state and implementation before every test. Equivalent to calling [`jest.restoreAllMocks()`](JestObjectAPI.md#jestrestoreallmocks) before each test. This will lead to any mocks having their fake implementations removed and restores their initial implementation.
740740

741741
### `rootDir` \[string]
742742

website/versioned_docs/version-25.x/MockFunctionAPI.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,20 @@ mockFn.mock.instances[1] === b; // true
8181

8282
### `mockFn.mockClear()`
8383

84-
Resets 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.
84+
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.
8585

86-
Beware that `mockClear` will replace `mockFn.mock`, not just these three properties! 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#clearmocks-boolean) configuration option is available to clear mocks automatically between tests.
86+
Beware that `mockClear` will replace `mockFn.mock`, not just these three properties! You should, therefore, avoid assigning `mockFn.mock` to other variables, temporary or not, to make sure you don't access stale data.
87+
88+
The [`clearMocks`](configuration#clearmocks-boolean) configuration option is available to clear mocks automatically before each tests.
8789

8890
### `mockFn.mockReset()`
8991

9092
Does everything that [`mockFn.mockClear()`](#mockfnmockclear) does, and also removes any mocked return values or implementations.
9193

9294
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).
9395

96+
The [`mockReset`](configuration#resetmocks-boolean) configuration option is available to reset mocks automatically before each test.
97+
9498
### `mockFn.mockRestore()`
9599

96100
Does everything that [`mockFn.mockReset()`](#mockfnmockreset) does, and also restores the original (non-mocked) implementation.
@@ -99,7 +103,7 @@ This is useful when you want to mock functions in certain test cases and restore
99103

100104
Beware that `mockFn.mockRestore` only works when the mock was created with `jest.spyOn`. Thus you have to take care of restoration yourself when manually assigning `jest.fn()`.
101105

102-
The [`restoreMocks`](configuration#restoremocks-boolean) configuration option is available to restore mocks automatically between tests.
106+
The [`restoreMocks`](configuration#restoremocks-boolean) configuration option is available to restore mocks automatically before each test.
103107

104108
### `mockFn.mockImplementation(fn)`
105109

0 commit comments

Comments
 (0)