Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Improve unstable_mockModule doc
- Chagned to the suggested version.
  • Loading branch information
iamWing committed Sep 2, 2022
commit a0f927139139a632582f95a24af2a421b9ddb7ec
4 changes: 3 additions & 1 deletion docs/ECMAScriptModules.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ import.meta.jest.useFakeTimers();

Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last idea from my side:

Suggested change
Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules.
## Module mocking in ESM
Since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we have this title added on v27+ only? We can only mock CJS modules before v27 even with ESM support enabled right? Plus I've noticed that the ECMAScript Modules page doesn't show up on v25's sidebar. Is that list a generated one or we have to update the sidebars.json manually?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it makes sense only with v27+. I will take a look at the sidebars later. Can it be there was no ESM support in v25? No sure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked around quickly. Seems like first steps with ESM support were released in v25. So you are right here too, that link is missing in the sidebar and must be added. Thanks!


Please also note that we currently don't support `jest.mock` in a clean way in ESM, but that is something we intend to add proper support for in the future. Follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below:
ESM module mocking is supported through `jest.unstable_mockModule`. As the name suggest works are in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates.

The usage of `jest.unstable_mockModule` is essentially the same as `jest.mock` with two differences: the factory function is required and it can be sync or async:

```js
import {jest} from '@jest/globals';
Expand Down
4 changes: 3 additions & 1 deletion website/versioned_docs/version-27.x/ECMAScriptModules.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ jest.useFakeTimers();

Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules.

Please also note that we currently don't support `jest.mock` in a clean way in ESM, but that is something we intend to add proper support for in the future. Follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below:
ESM module mocking is supported through `jest.unstable_mockModule`. As the name suggest works are in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates.

The usage of `jest.unstable_mockModule` is essentially the same as `jest.mock` with two differences: the factory function is required and it can be sync or async:

```js
import {jest} from '@jest/globals';
Expand Down
4 changes: 3 additions & 1 deletion website/versioned_docs/version-28.x/ECMAScriptModules.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ import.meta.jest.useFakeTimers();

Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules.

Please also note that we currently don't support `jest.mock` in a clean way in ESM, but that is something we intend to add proper support for in the future. Follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below:
ESM module mocking is supported through `jest.unstable_mockModule`. As the name suggest works are in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates.

The usage of `jest.unstable_mockModule` is essentially the same as `jest.mock` with two differences: the factory function is required and it can be sync or async:

```js
import {jest} from '@jest/globals';
Expand Down
4 changes: 3 additions & 1 deletion website/versioned_docs/version-29.0/ECMAScriptModules.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ import.meta.jest.useFakeTimers();

Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules.

Please also note that we currently don't support `jest.mock` in a clean way in ESM, but that is something we intend to add proper support for in the future. Follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below:
ESM module mocking is supported through `jest.unstable_mockModule`. As the name suggest works are in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates.

The usage of `jest.unstable_mockModule` is essentially the same as `jest.mock` with two differences: the factory function is required and it can be sync or async:

```js
import {jest} from '@jest/globals';
Expand Down