Skip to content

Commit dbd82dc

Browse files
silvenoncpojer
authored andcommitted
Document doMock and dontMock (#3919)
1 parent 0f66444 commit dbd82dc

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

Diff for: docs/en/JestObjectAPI.md

+40-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ The `jest` object is automatically in scope within every test file. The methods
1919
- [`jest.isMockFunction(fn)`](#jestismockfunctionfn)
2020
- [`jest.genMockFromModule(moduleName)`](#jestgenmockfrommodulemodulename)
2121
- [`jest.mock(moduleName, factory, options)`](#jestmockmodulename-factory-options)
22+
- [`jest.unmock(moduleName)`](#jestunmockmodulename)
23+
- [`jest.doMock(moduleName, factory, options)`](#jestdomockmodulename-factory-options)
24+
- [`jest.dontMock(moduleName)`](#jestdontmockmodulename)
2225
- [`jest.clearAllMocks()`](#jestclearallmocks)
2326
- [`jest.resetAllMocks()`](#jestresetallmocks)
2427
- [`jest.resetModules()`](#jestresetmodules)
@@ -28,7 +31,6 @@ The `jest` object is automatically in scope within every test file. The methods
2831
- [`jest.runOnlyPendingTimers()`](#jestrunonlypendingtimers)
2932
- [`jest.setMock(moduleName, moduleExports)`](#jestsetmockmodulename-moduleexports)
3033
- [`jest.setTimeout(timeout)`](#jestsettimeouttimeout)
31-
- [`jest.unmock(moduleName)`](#jestunmockmodulename)
3234
- [`jest.useFakeTimers()`](#jestusefaketimers)
3335
- [`jest.useRealTimers()`](#jestuserealtimers)
3436
- [`jest.spyOn(object, methodName)`](#jestspyonobject-methodname)
@@ -120,14 +122,49 @@ jest.mock('../moduleName', () => {
120122
}, {virtual: true});
121123
```
122124

123-
*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-
125125
*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.*
126126

127127
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.
128128

129129
Returns the `jest` object for chaining.
130130

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+
return jest.fn(() => 1);
147+
});
148+
const moduleName = require('../moduleName');
149+
expect(moduleName()).toEqual(1);
150+
});
151+
152+
test('moduleName 2', () => {
153+
jest.doMock('../moduleName', () => {
154+
return jest.fn(() => 2);
155+
});
156+
const moduleName = 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+
131168
### `jest.clearAllMocks()`
132169
Clears the `mock.calls` and `mock.instances` properties of all mocks. Equivalent to calling `.mockClear()` on every mocked function.
133170

@@ -217,15 +254,6 @@ Example:
217254
jest.setTimeout(1000); // 1 second
218255
```
219256

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-
229257
### `jest.useFakeTimers()`
230258
Instructs Jest to use fake versions of the standard timer functions (`setTimeout`, `setInterval`, `clearTimeout`, `clearInterval`, `nextTick`, `setImmediate` and `clearImmediate`).
231259

0 commit comments

Comments
 (0)