New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(jest.doMock): Add information for using ES6 modules with doMock #8573
Changes from 3 commits
005cafe
024868d
e255a40
10d961f
fcf22bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -314,6 +314,46 @@ test('moduleName 2', () => { | |
}); | ||
``` | ||
|
||
Using `jest.doMock()` with ES6 modules requires additional steps: | ||
|
||
- We have to specify the `__esModule: true` property (see the [`jest.mock()`](#jestmockmodulename-factory-options) API for more information). | ||
- Static ES6 module imports are hoisted to the top of the file, so instead we have to import them dynamically using `import()`. | ||
- Finally, we need an environment which supports dynamic importing. Please see [Using Babel](GettingStarted.md#using-babel) for the initial setup. Then add the plugin [babel-plugin-dynamic-import-node](https://www.npmjs.com/package/babel-plugin-dynamic-import-node), or an equivalent, to your Babel config to enable dynamic importing in Node. | ||
|
||
```js | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
}); | ||
|
||
test('moduleName 1', () => { | ||
jest.doMock('../moduleName', () => { | ||
return { | ||
__esModule: true, | ||
default: 'default1', | ||
foo: 'foo1', | ||
}; | ||
}); | ||
import('../moduleName').then(moduleName => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please either use async/await or return this promise from test – otherwise it's not gonna work properly. Same for the test below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes you're correct. Surprisingly these worked for me anyway (i.e. when changing the value in Changed. |
||
expect(moduleName.default).toEqual('default1'); | ||
expect(moduleName.foo).toEqual('foo1'); | ||
}); | ||
}); | ||
|
||
test('moduleName 2', () => { | ||
jest.doMock('../moduleName', () => { | ||
return { | ||
__esModule: true, | ||
default: 'default2', | ||
foo: 'foo2', | ||
}; | ||
}); | ||
import('../moduleName').then(moduleName => { | ||
expect(moduleName.default).toEqual('default2'); | ||
expect(moduleName.foo).toEqual('foo2'); | ||
}); | ||
}); | ||
``` | ||
|
||
Returns the `jest` object for chaining. | ||
|
||
### `jest.dontMock(moduleName)` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it doesn't. Using
jest.doMock
with ES6import
s does. Please clarify this, because people may get wrong idea that they need to install some babel plugin to actually test a module.This should clearly state something like: "use if you don't want to
require
your ES6 modules"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, changed.