Skip to content

Commit 3748557

Browse files
sargaliasthymikee
authored andcommitted
docs(jest-mock): Add example for ES6 modules (#8550)
* docs(JestObjectAPI jest-mock): Add example for ES6 modules Issue #8543 * chore(CHANGELOG): Update CHANGELOG * docs(JestObjectAPI jest-mock): Add explanation for __esModule: true Issue #8543 * docs(JestObjectAPI jest-mock): Update versioned_docs Issue #8543 * Revert "docs(JestObjectAPI jest-mock): Update versioned_docs" This reverts commit ba17c08. Updating the versioned_docs for version 24.6 is not necessary as we can update an earlier version and the changes will propagate to the new version. * docs(JestObjectAPI jest-mock): Update previous versioned_docs The example for using __esModule: true is also relevant for those versions, so it should be present.
1 parent be1dda3 commit 3748557

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
- `[jest-leak-detector]` remove code repeat ([#8438](https://github.com/facebook/jest/pull/8438)
1919
- `[docs]` Add example to `jest.requireActual` ([#8482](https://github.com/facebook/jest/pull/8482)
20+
- `[docs]` Add example to `jest.mock` for mocking ES6 modules with the `factory` parameter ([#8550](https://github.com/facebook/jest/pull/8550))
2021

2122
### Performance
2223

docs/JestObjectAPI.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,23 @@ const moduleName = require('../moduleName');
240240
moduleName(); // Will return '42';
241241
```
242242

243+
When using the `factory` parameter for an ES6 module with a default export, the `__esModule: true` property needs to be specified. This property is normally generated by Babel / TypeScript, but here it needs to be set manually. When importing a default export, it's an instruction to import the property named `default` from the export object:
244+
245+
```js
246+
import moduleName, {foo} from '../moduleName';
247+
248+
jest.mock('../moduleName', () => {
249+
return {
250+
__esModule: true,
251+
default: jest.fn(() => 42),
252+
foo: jest.fn(() => 43),
253+
};
254+
});
255+
256+
moduleName(); // Will return 42
257+
foo(); // Will return 43
258+
```
259+
243260
The third argument can be used to create virtual mocks – mocks of modules that don't exist anywhere in the system:
244261

245262
```js

website/versioned_docs/version-22.x/JestObjectAPI.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,23 @@ const moduleName = require('../moduleName');
294294
moduleName(); // Will return '42';
295295
```
296296

297+
When using the `factory` parameter for an ES6 module with a default export, the `__esModule: true` property needs to be specified. This property is normally generated by Babel / TypeScript, but here it needs to be set manually. When importing a default export, it's an instruction to import the property named `default` from the export object:
298+
299+
```js
300+
import moduleName, {foo} from '../moduleName';
301+
302+
jest.mock('../moduleName', () => {
303+
return {
304+
__esModule: true,
305+
default: jest.fn(() => 42),
306+
foo: jest.fn(() => 43),
307+
};
308+
});
309+
310+
moduleName(); // Will return 42
311+
foo(); // Will return 43
312+
```
313+
297314
The third argument can be used to create virtual mocks – mocks of modules that don't exist anywhere in the system:
298315

299316
```js

website/versioned_docs/version-23.x/JestObjectAPI.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,23 @@ const moduleName = require('../moduleName');
295295
moduleName(); // Will return '42';
296296
```
297297

298+
When using the `factory` parameter for an ES6 module with a default export, the `__esModule: true` property needs to be specified. This property is normally generated by Babel / TypeScript, but here it needs to be set manually. When importing a default export, it's an instruction to import the property named `default` from the export object:
299+
300+
```js
301+
import moduleName, {foo} from '../moduleName';
302+
303+
jest.mock('../moduleName', () => {
304+
return {
305+
__esModule: true,
306+
default: jest.fn(() => 42),
307+
foo: jest.fn(() => 43),
308+
};
309+
});
310+
311+
moduleName(); // Will return 42
312+
foo(); // Will return 43
313+
```
314+
298315
The third argument can be used to create virtual mocks – mocks of modules that don't exist anywhere in the system:
299316

300317
```js

website/versioned_docs/version-24.0/JestObjectAPI.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,23 @@ const moduleName = require('../moduleName');
241241
moduleName(); // Will return '42';
242242
```
243243

244+
When using the `factory` parameter for an ES6 module with a default export, the `__esModule: true` property needs to be specified. This property is normally generated by Babel / TypeScript, but here it needs to be set manually. When importing a default export, it's an instruction to import the property named `default` from the export object:
245+
246+
```js
247+
import moduleName, {foo} from '../moduleName';
248+
249+
jest.mock('../moduleName', () => {
250+
return {
251+
__esModule: true,
252+
default: jest.fn(() => 42),
253+
foo: jest.fn(() => 43),
254+
};
255+
});
256+
257+
moduleName(); // Will return 42
258+
foo(); // Will return 43
259+
```
260+
244261
The third argument can be used to create virtual mocks – mocks of modules that don't exist anywhere in the system:
245262

246263
```js

0 commit comments

Comments
 (0)