0
# Mock Management
1
2
Utilities for managing mock state, resetting mocks between tests, and controlling global mocking behavior. Essential for maintaining clean test environments and proper Jest integration.
3
4
## Capabilities
5
6
### Reset Mocks
7
8
Resets all mock state including call history, implementations, and returns to default mocking behavior.
9
10
```javascript { .api }
11
/**
12
* Reset all mock state and return to default behavior
13
* @returns void
14
*/
15
fetch.resetMocks();
16
```
17
18
**Usage Examples:**
19
20
```javascript
21
// Reset between tests
22
beforeEach(() => {
23
fetch.resetMocks();
24
});
25
26
// Reset during a test
27
it("handles multiple scenarios", () => {
28
fetch.mockResponse("First scenario");
29
// ... test first scenario
30
31
fetch.resetMocks();
32
fetch.mockResponse("Second scenario");
33
// ... test second scenario
34
});
35
36
// Check what resetMocks does:
37
// - Clears fetch.mock.calls
38
// - Clears fetch.mock.results
39
// - Resets to default mockImplementation
40
// - Re-enables mocking (calls doMock())
41
```
42
43
### Enable Mocks
44
45
Enables fetch mocking globally by replacing the global fetch with the mock version.
46
47
```javascript { .api }
48
/**
49
* Enable fetch mocking globally (replace global fetch with mock)
50
* @returns void
51
*/
52
fetch.enableMocks();
53
```
54
55
**Usage Examples:**
56
57
```javascript
58
// In setupJest.js or test setup file
59
require("jest-fetch-mock").enableMocks();
60
61
// Or using the alias
62
const { enableFetchMocks } = require("jest-fetch-mock");
63
enableFetchMocks();
64
65
// What enableMocks does:
66
// - Sets global.fetch = fetchMock
67
// - Sets global.fetchMock = fetchMock
68
// - Attempts to mock 'node-fetch' module for Node.js compatibility
69
```
70
71
### Enable Fetch Mocks (Alias)
72
73
Convenient alias for `enableMocks` providing the same functionality.
74
75
```javascript { .api }
76
/**
77
* Alias for enableMocks - enable fetch mocking globally
78
* @returns void
79
*/
80
fetch.enableFetchMocks();
81
```
82
83
### Disable Mocks
84
85
Disables fetch mocking globally by restoring the original fetch implementation.
86
87
```javascript { .api }
88
/**
89
* Disable fetch mocking globally (restore original fetch)
90
* @returns void
91
*/
92
fetch.disableMocks();
93
```
94
95
**Usage Examples:**
96
97
```javascript
98
// Temporarily disable mocking for integration tests
99
beforeAll(() => {
100
fetch.disableMocks();
101
});
102
103
afterAll(() => {
104
fetch.enableMocks();
105
});
106
107
// What disableMocks does:
108
// - Restores global.fetch to original implementation (cross-fetch)
109
// - Stops mocking 'node-fetch' module
110
```
111
112
### Disable Fetch Mocks (Alias)
113
114
Convenient alias for `disableMocks`.
115
116
```javascript { .api }
117
/**
118
* Alias for disableMocks - disable fetch mocking globally
119
* @returns void
120
*/
121
fetch.disableFetchMocks();
122
```
123
124
## Named Export Functions
125
126
These functions are available as named exports for ES6 import syntax and provide the same functionality as the methods above.
127
128
### Enable Fetch Mocks (Named Export)
129
130
```javascript { .api }
131
/**
132
* Enable fetch mocking globally (named export function)
133
* @returns void
134
*/
135
function enableFetchMocks();
136
```
137
138
**Usage Examples:**
139
140
```javascript
141
// ES6 import syntax
142
import { enableFetchMocks } from "jest-fetch-mock";
143
144
// Enable in setup
145
enableFetchMocks();
146
```
147
148
### Disable Fetch Mocks (Named Export)
149
150
```javascript { .api }
151
/**
152
* Disable fetch mocking globally (named export function)
153
* @returns void
154
*/
155
function disableFetchMocks();
156
```
157
158
**Usage Examples:**
159
160
```javascript
161
// ES6 import syntax
162
import { disableFetchMocks } from "jest-fetch-mock";
163
164
// Disable when needed
165
disableFetchMocks();
166
```
167
168
## Jest Integration
169
170
### Setup Patterns
171
172
**Option 1: Global Setup (setupJest.js)**
173
174
```javascript
175
// setupJest.js
176
require("jest-fetch-mock").enableMocks();
177
178
// Or with ES6
179
import { enableFetchMocks } from "jest-fetch-mock";
180
enableFetchMocks();
181
```
182
183
**Option 2: Per-Test File Setup**
184
185
```javascript
186
// At top of test file
187
import fetchMock from "jest-fetch-mock";
188
fetchMock.enableMocks();
189
190
beforeEach(() => {
191
fetchMock.resetMocks();
192
});
193
```
194
195
**Option 3: Default Disabled**
196
197
```javascript
198
// setupJest.js - available but not enabled by default
199
require("jest-fetch-mock").enableMocks();
200
fetchMock.dontMock(); // Use real fetch by default
201
202
// In specific tests, enable as needed
203
beforeEach(() => {
204
fetchMock.doMock();
205
});
206
```
207
208
### Jest Configuration
209
210
```json
211
{
212
"jest": {
213
"automock": false,
214
"setupFiles": ["./setupJest.js"]
215
}
216
}
217
```
218
219
### Mock Inspection
220
221
Since fetchMock is a Jest mock function, you have access to all Jest mock properties:
222
223
```javascript
224
// Call history
225
expect(fetch.mock.calls).toHaveLength(2);
226
expect(fetch.mock.calls[0][0]).toBe("/api/users");
227
expect(fetch.mock.calls[1][0]).toBe("/api/posts");
228
229
// Call arguments
230
const [url, options] = fetch.mock.calls[0];
231
expect(url).toBe("/api/data");
232
expect(options.method).toBe("POST");
233
234
// Results
235
expect(fetch.mock.results[0].type).toBe("return");
236
237
// Mock instance properties
238
expect(fetch.mock.instances).toHaveLength(2);
239
```
240
241
## Best Practices
242
243
### Test Isolation
244
245
```javascript
246
describe("API tests", () => {
247
beforeEach(() => {
248
fetch.resetMocks(); // Clean slate for each test
249
});
250
251
afterEach(() => {
252
// Optional: additional cleanup
253
expect(fetch.mock.calls).toBeDefined();
254
});
255
});
256
```
257
258
### Environment-Specific Setup
259
260
```javascript
261
// setupJest.js
262
if (process.env.NODE_ENV === "test") {
263
require("jest-fetch-mock").enableMocks();
264
265
// Default to mocking unless explicitly disabled
266
global.fetch.doMock();
267
}
268
```
269
270
### Integration Test Support
271
272
```javascript
273
// For integration tests that need real fetch
274
const originalFetch = global.fetch;
275
276
beforeAll(() => {
277
if (process.env.INTEGRATION_TESTS) {
278
fetch.disableMocks();
279
}
280
});
281
282
afterAll(() => {
283
if (process.env.INTEGRATION_TESTS) {
284
fetch.enableMocks();
285
}
286
});
287
```