Repository navigation
Description
Once Node 10 is EOL at the end of April, lots of libraries and modules will probably be written in native ESM rather than CJS. While we've mostly been focusing on getting tests running in ESM, we're sorta limited/slowed down by the fact the vm APIs are experimental and flagged on Node's side. That is not the case for "normal" ESM.
We already support user configuration written in ESM.
With that in mind, all modules in Jest that are "pluggable" should be able to load ESM.
-
dependencyExtractor(feat(jest-haste-map): supportdependencyExtractorwritten in ESM #12008) -
globalSetup(feat(jest-core): add support forglobalSetupandglobalTeardownwritten in ESM #11267) -
globalTeardown(feat(jest-core): add support forglobalSetupandglobalTeardownwritten in ESM #11267) -
preset(feat(jest-config): add support forpresetwritten in ESM #11200) -
prettierPath -
reporters(feat: support reporters written in ESM #11427) -
resolver(fix(jest-resolve): load a user resolver written as an ES module #16332) -
runner(feat: requireAndTranspileModule support ESM #11232) -
snapshotResolver(feat(snapshot): support snapshotResolver and snapshotSerializers written in ESM #12014) -
snapshotSerializers(feat(snapshot): support snapshotResolver and snapshotSerializers written in ESM #12014) -
testEnvironment(feature: allow ESM for test environment #11033) -
testResultsProcessor(feat(jest-core): supporttestResultsProcessorwritten in ESM #12006) -
testRunner(feat: requireAndTranspileModule support ESM #11232) -
testSequencer(feat(jest-core): Add support for testSequencer written in ESM #11207) -
transformers(feat(jest-transform): support transformers written in ESM #11163) -
watchPlugins(feat(jest-core): add support forwatchPluginswritten in ESM #11315)
Any and all help here would be greatly appreciated!
In general, it means attempting to require, and if that fails with an ESM error, use import() and verify it's the default export. Paired with an integration test that has the module in question written in ESM and verifying it works.
Whenever we drop Node 10 (probably for Jest 28) we can do just the import call as that can load both ESM and CJS, but that'll be a simple refactor as it's just removing the initial require and try-catch. So I still think it's worth it to add support now as the code difference is quite small and the later refactor is minimal
Activity
ahnpnl commentedon Mar 9, 2021
I think it’s wise to pin this issue so it stands out to everyone :)
SimenB commentedon Mar 9, 2021
sure 🙂
theoludwig commentedon Apr 22, 2021
I think we can already drop the support node 10 for
jest@27, there is no need to wait forjest@28as it will reach EOL. 👍ahnpnl commentedon Apr 30, 2021
D-Day for Node 10 is here xD
SimenB commentedon Apr 30, 2021
Jest 27 will support Node 10 so people who are stuck on older versions (of which there are many) can use it
gilles-yvetot commentedon May 3, 2021
@SimenB you listed
globalSetupas one of thing to migrate to ESM but I don't seesetupFiles. Is it an omission or it will be taken care withglobalSetup?SimenB commentedon May 4, 2021
setupFilesare part of the test run, so that already works 🙂gilles-yvetot commentedon May 4, 2021
Maybe that's one of the modules I am using but I cannot use ESM in my
setupFileswith the current version27.0.0-next.9SimenB commentedon May 4, 2021
Could you open up a new issue with a reproduction?
SimenB commentedon May 18, 2021
I want to do
reportersandresolver, then release v27. Anyone up for sending PRs for those?reporters: https://github.com/facebook/jest/blob/a4358d619131498287e25a2c2f604f813481ddeb/packages/jest-core/src/TestScheduler.ts#L403resolver: A bit more involved as we'll need to load the resolver ahead of time instead of on demand. I.e. passing aresolverModuleoption in addition toresolver? The place it'srequired today needs to stay synchronous. https://github.com/facebook/jest/blob/master/packages/jest-resolve/src/index.ts#L108akauppi commentedon May 19, 2021
Hi @gilles-yvetot
If you want to fix the
setupFilesthing, you can compare your approach to what I'm doing in firebase-jest-testing. It uses both Jest 27.0.0-next.9 and ESMglobalSetup.5 remaining items
thernstig commentedon May 26, 2021
I tested it, and it does work 😆
chentsulin commentedon Oct 28, 2021
Hi @SimenB,
I just try to help doing these parts:
After I dived into it, I found there're some problems that I can't solve by myself.
dependencyExtractor
dependencyExtractoris used in the constructor:https://github.com/facebook/jest/blob/3d04f33daa3b9ec21838c9177ab7acab8c983155/packages/jest-haste-map/src/index.ts#L241-L250
So the only chance I could find is to make the whole HasteMap factory async like this:
Is this a right approach?
prettierPath
prettierPathis required using a helper calledrequireOutside:https://github.com/facebook/jest/blob/3d04f33daa3b9ec21838c9177ab7acab8c983155/packages/jest-snapshot/src/InlineSnapshots.ts#L53-L60
This
requireOutsidefunction will be transformed bybabel-plugin-jest-require-outside-vm:https://github.com/facebook/jest/blob/3d04f33daa3b9ec21838c9177ab7acab8c983155/scripts/babel-plugin-jest-require-outside-vm.js#L13-L21
I currently don't have enough knowledge about how to handle this with ESModules.
snapshotResolver & snapshotSerializers
Those two are relying on the
localRequirefunction:https://github.com/facebook/jest/blob/3d04f33daa3b9ec21838c9177ab7acab8c983155/packages/jest-snapshot/src/SnapshotResolver.ts#L79-L85
https://github.com/facebook/jest/blob/3d04f33daa3b9ec21838c9177ab7acab8c983155/packages/jest-jasmine2/src/setup_jest_globals.ts#L95-L104
So, should we pass
runtime.unstable_importModule.bind(runtime)aslocalImportto solve this problem?https://github.com/facebook/jest/blob/3d04f33daa3b9ec21838c9177ab7acab8c983155/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts#L34
https://github.com/facebook/jest/blob/3d04f33daa3b9ec21838c9177ab7acab8c983155/packages/jest-runtime/src/index.ts#L622
SimenB commentedon Oct 28, 2021
Wonderful, thanks!
Yeah, that seems correct. Makes it a breaking change though, so need to wait for Jest 28.
Good point, we might have to leave that alone. This is for a specific module (
prettier) though, so not as likely that people plug in their own thing which is ESM. If (when)prettiermigrates to ESM only, we must change, but up until that leaving it asrequireseems sensible.Yeah, passing in
unstable_importModulemakes sense. Should probably also pass in https://github.com/facebook/jest/blob/3d04f33daa3b9ec21838c9177ab7acab8c983155/packages/jest-runtime/src/index.ts#L421chentsulin commentedon Oct 28, 2021
Passing
localRequire,unstable_importModuleandunstable_shouldLoadAsEsmthrough multiple layers looks a little bit tricky and breaks function signature unavoidably.How about replacing
localRequirewith alocalRequireOrImportModulefunction?SimenB commentedon Oct 28, 2021
That should work as well 👍
F3n67u commentedon Apr 24, 2022
@SimenB is there any work left to do? I am very glad to push these things forward.
SimenB commentedon Apr 24, 2022
Great! The ones not crossed out in the OP are still missing. Not sure how feasible they are
damianobarbati commentedon Oct 10, 2022
@SimenB is this waiting for nodejs team to provide support?
RanylFoumbi commentedon Apr 25, 2023
HI @SimenB
i'm currently facing badly issue using jest ES6 to mock axios in nodejs
jest doesn't mock axios like when using ESM like it was when using require to import module
Please take a look here axios/axios#5676 (comment)
carlocorradini commentedon Aug 16, 2023
Is it possible to make
expectESM compatible?ESM compatibility is worthwhile since it can be installed as a separate module/dependency.
Anutrix commentedon Sep 21, 2023
Prettier seems to have moved on to ESM: https://prettier.io/blog/2023/07/05/3.0.0.html.
usrrname commentedon Sep 25, 2023
🎻
feat(snapshot): load snapshotResolver and snapshotSerializers outside…