You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*Note: When using `babel-jest`, calls to `mock` will automatically be hoisted to the top of the code block. Use `doMock` if you want to explicitly avoid this behavior.*
124
-
125
125
*Warning: Importing a module in a setup file (as specified by `setupTestFrameworkScriptFile`) will prevent mocking for the module in question, as well as all the modules that it imports.*
126
126
127
127
Modules that are mocked with `jest.mock` are mocked only for the file that calls `jest.mock`. Another file that imports the module will get the original implementation even if run after the test file that mocks the module.
128
128
129
129
Returns the `jest` object for chaining.
130
130
131
+
### `jest.unmock(moduleName)`
132
+
Indicates that the module system should never return a mocked version of the specified module from `require()` (e.g. that it should always return the real module).
133
+
134
+
The most common use of this API is for specifying the module a given test intends to be testing (and thus doesn't want automatically mocked).
135
+
136
+
Returns the `jest` object for chaining.
137
+
138
+
### `jest.doMock(moduleName, factory, options)`
139
+
When using `babel-jest`, calls to `mock` will automatically be hoisted to the top of the code block. Use this method if you want to explicitly avoid this behavior.
140
+
141
+
This is useful when you want to mock a module differently within a same file:
142
+
143
+
```js
144
+
test('moduleName 1', () => {
145
+
jest.doMock('../moduleName', () => {
146
+
returnjest.fn(() =>1);
147
+
});
148
+
constmoduleName=require('../moduleName');
149
+
expect(moduleName()).toEqual(1);
150
+
});
151
+
152
+
test('moduleName 2', () => {
153
+
jest.doMock('../moduleName', () => {
154
+
returnjest.fn(() =>2);
155
+
});
156
+
constmoduleName=require('../moduleName');
157
+
expect(moduleName()).toEqual(2);
158
+
});
159
+
```
160
+
161
+
Returns the `jest` object for chaining.
162
+
163
+
### `jest.dontMock(moduleName)`
164
+
When using `babel-jest`, calls to `unmock` will automatically be hoisted to the top of the code block. Use this method if you want to explicitly avoid this behavior.
165
+
166
+
Returns the `jest` object for chaining.
167
+
131
168
### `jest.clearAllMocks()`
132
169
Clears the `mock.calls` and `mock.instances` properties of all mocks. Equivalent to calling `.mockClear()` on every mocked function.
133
170
@@ -217,15 +254,6 @@ Example:
217
254
jest.setTimeout(1000); // 1 second
218
255
```
219
256
220
-
### `jest.unmock(moduleName)`
221
-
Indicates that the module system should never return a mocked version of the specified module from `require()` (e.g. that it should always return the real module).
222
-
223
-
The most common use of this API is for specifying the module a given test intends to be testing (and thus doesn't want automatically mocked).
224
-
225
-
Returns the `jest` object for chaining.
226
-
227
-
*Note: this method was previously called `dontMock`. When using `babel-jest`, calls to `unmock` will automatically be hoisted to the top of the code block. Use `dontMock` if you want to explicitly avoid this behavior.*
228
-
229
257
### `jest.useFakeTimers()`
230
258
Instructs Jest to use fake versions of the standard timer functions (`setTimeout`, `setInterval`, `clearTimeout`, `clearInterval`, `nextTick`, `setImmediate` and `clearImmediate`).
0 commit comments