-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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: ESM update #13191
docs: ESM update #13191
Conversation
- Added link to `ECMAScriptModules` in section `Using with ES module imports`.
- To reflect the most updated status.
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.
Thanks!
Yes, it would be very useful to have jest.unstable_mock()
documented as well. If a paragraph on jest.mock()
is added, then someone trying to mock ESM module might be not happy (;
By the way, |
Okay. One question tho, am I right that |
Oh.. Sorry there was a typo. The method is called Your example with |
- Also clarified that the limitation comes from ESM, not Jest.
@mrazauskas I've added a fairly simple example for |
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
docs/ECMAScriptModules.md
Outdated
@@ -38,4 +38,51 @@ import.meta.jest.useFakeTimers(); | |||
// jest === import.meta.jest => true | |||
``` | |||
|
|||
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 dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked 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.
why do we talk about "dynamic import()
", and then go on to use require
?
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.
I've made the change to also mention require
there, with the example now showing both require
and dynamic import()
usage. Personally I prefer require
for CJS modules but I think it's better to show both on the docs so the users aren't going to think that either is more preferable.
Thanks for your patience and collaboration. This is shaping up nicely! |
- Diabled `no-redeclare`.
- Chagned to the suggested version.
Yeah we're getting close to finalise it. I do think this doc is much needed for people migrating to ESM syntax. In fact, a fellow developer using my Electron boilerplate with ESM enabled got confused with this recently. There should be a lot less confusion for the users once we have the updated doc goes live. |
docs/ECMAScriptModules.md
Outdated
@@ -38,4 +38,57 @@ import.meta.jest.useFakeTimers(); | |||
// jest === import.meta.jest => true | |||
``` | |||
|
|||
Please 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. | |||
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. |
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.
Last idea from my side:
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. |
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.
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?
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.
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.
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.
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!
- Added section title `MOdule mocking in ESM` on `v27+` only.
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.
this is looking great! just some minor nits
thanks! |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
As discussed in #13135, this PR:
jest.mock
with ESM support activated and added example of putting themock
calls beforeimport
statements to properly load the import modules.Using with ES module imports
inManulMocks.md
to point out the hoisting mentioned there does not applied when ESM support is activated with link toECMAScriptModules.md
.Additionally, I've updated the Node version number mentioned in
ECMAScriptModules.md
to reflect the fact thatvm.Module
is still considered experimental in18.8.0
.@mrazauskas I've found that already a link to #10025 in
ECMAScriptModules.md
, do you want to add some documentations forjest.unstable_mock
there as well or you think it's fine just leave it like this for now?Test plan
No test plan. Only docs are updated.