diff --git a/CHANGELOG.md b/CHANGELOG.md index d3e3025a0511..64bbacc3b30d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - `[jest-leak-detector]` remove code repeat ([#8438](https://github.com/facebook/jest/pull/8438) - `[docs]` Add example to `jest.requireActual` ([#8482](https://github.com/facebook/jest/pull/8482) +- `[docs]` Add example to `jest.mock` for mocking ES6 modules with the `factory` parameter ([#8550](https://github.com/facebook/jest/pull/8550)) ### Performance diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index 1eef66de74b7..bf73f02b69cd 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -240,6 +240,23 @@ const moduleName = require('../moduleName'); moduleName(); // Will return '42'; ``` +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: + +```js +import moduleName, {foo} from '../moduleName'; + +jest.mock('../moduleName', () => { + return { + __esModule: true, + default: jest.fn(() => 42), + foo: jest.fn(() => 43), + }; +}); + +moduleName(); // Will return 42 +foo(); // Will return 43 +``` + The third argument can be used to create virtual mocks – mocks of modules that don't exist anywhere in the system: ```js diff --git a/website/versioned_docs/version-22.x/JestObjectAPI.md b/website/versioned_docs/version-22.x/JestObjectAPI.md index 8fbe46f7cf85..6f1c9e765882 100644 --- a/website/versioned_docs/version-22.x/JestObjectAPI.md +++ b/website/versioned_docs/version-22.x/JestObjectAPI.md @@ -294,6 +294,23 @@ const moduleName = require('../moduleName'); moduleName(); // Will return '42'; ``` +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: + +```js +import moduleName, {foo} from '../moduleName'; + +jest.mock('../moduleName', () => { + return { + __esModule: true, + default: jest.fn(() => 42), + foo: jest.fn(() => 43), + }; +}); + +moduleName(); // Will return 42 +foo(); // Will return 43 +``` + The third argument can be used to create virtual mocks – mocks of modules that don't exist anywhere in the system: ```js diff --git a/website/versioned_docs/version-23.x/JestObjectAPI.md b/website/versioned_docs/version-23.x/JestObjectAPI.md index 52b2b6fd19dd..62ec5e649334 100644 --- a/website/versioned_docs/version-23.x/JestObjectAPI.md +++ b/website/versioned_docs/version-23.x/JestObjectAPI.md @@ -295,6 +295,23 @@ const moduleName = require('../moduleName'); moduleName(); // Will return '42'; ``` +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: + +```js +import moduleName, {foo} from '../moduleName'; + +jest.mock('../moduleName', () => { + return { + __esModule: true, + default: jest.fn(() => 42), + foo: jest.fn(() => 43), + }; +}); + +moduleName(); // Will return 42 +foo(); // Will return 43 +``` + The third argument can be used to create virtual mocks – mocks of modules that don't exist anywhere in the system: ```js diff --git a/website/versioned_docs/version-24.0/JestObjectAPI.md b/website/versioned_docs/version-24.0/JestObjectAPI.md index 56f81714d73f..5ddf386f97b3 100644 --- a/website/versioned_docs/version-24.0/JestObjectAPI.md +++ b/website/versioned_docs/version-24.0/JestObjectAPI.md @@ -241,6 +241,23 @@ const moduleName = require('../moduleName'); moduleName(); // Will return '42'; ``` +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: + +```js +import moduleName, {foo} from '../moduleName'; + +jest.mock('../moduleName', () => { + return { + __esModule: true, + default: jest.fn(() => 42), + foo: jest.fn(() => 43), + }; +}); + +moduleName(); // Will return 42 +foo(); // Will return 43 +``` + The third argument can be used to create virtual mocks – mocks of modules that don't exist anywhere in the system: ```js