-
-
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
Please consider adding native support for ES modules #4842
Comments
Jest has its own It might be possible to automatically transpile |
I agree with SimenB. We need a number of hooks from the node team to be able to make this work together with the vm module. However, I think we should support this in the meantime, but not by mirroring the full native implementation but rather by using babel and compiling it to require inside of |
So just adding |
Yeah, that's what I was thinking. |
Guys, what about integrating https://github.com/standard-things/esm ? It's fast and maintain a lot of edge cases. |
@TrySound what would that look like concretely? Can you make a prototype? |
We still have our own require-implementation (needed for mocks), so I don't think that'd help much. And we need to work both with Node's rules and the browser's rules. I'd be very happy to be corrected and have it work perfectly for us :D |
Could anyone give it a whirl and come back with a PR for the docs? 🙂 |
I guess users would like to have support everywhere, but I found it works only for dependencies of test files. // test.js
require = require('@std/esm')(module, { esm: 'js', cjs: true });
const utils = require('./utils'); // utils.js
export { default as update } from './update'; It's better, but not ideal. |
I don't think this is a great solution, as it doesn't perform any "missing export" checks that are so valuable with ES modules. For example, in React repo I started running build more often just because Rollup finds these mistakes but Jest with
It would be pretty great to invest some time into their interop. Pretty sure this hacky solution will break eventually: https://stackoverflow.com/questions/46433678/specify-code-to-run-before-any-jest-setup-happens. But if it's just a matter of exposing something on Jest side, would be cool to see it supported. |
@SimenB, in my mind the immediate step would not be too complex. What is urgent is to allow people to work with .mjs module, even if babel is helping behind the test scene. Otherwise people might have to find different testing solution if they want to use .mjs.
The final solution might be complex and takes time, but it is inevitable isn't it? |
Hello, has someone been able to fix this error? |
We are using .mjs with "node --experimental-modules" option. Any workaround? |
That's experimental and not fully fleshed out. There is lots of churn still with basic things, like how to import a builtin module, still up in the air. Projects like AVA have started allowing the use of |
Supporting |
@SimenB Can you chat sometime on hangouts? |
Hi @SimenB 👋 An Update: The |
That's pretty cool! Thanks for sharing. We'll have to figure out where we want the integration to be. How would it handle CSS (or other non-js assets) files? Should it just be a transform? What about the built-in babel transform? How should Jest behave when it comes to the incoming loaders, if it does affect anything? |
It seems like there may be a benefit for a community contrib |
I'm sure if you can get the example code @jdalton linked to to work without issues (or close to it), it should be simple enough to load in the esm loader behind a flag in jest itself. One thing I see as an issue is that it wants the real |
May just need some tweaking of how Jest taps into CJS. For example when mocking the Update: I'm now working on enabling Jest out of the box with |
@jdalton Any update with the |
Hi @JasonCust, wow my comment has gotten some attention! I have made progress in identifying the work needed to enable Jest support in |
Any rough timetable on this?
as such I am reluctant to export NPM packages (which may well be consumed by tests implementing the JEST testing framework) using commonJS for much longer. |
We're shipping an experimental version from Jest 25.4. Quite a few bug fixes in 25.5, but it's still not where it should be. You can follow the progress in #9430 |
wow, 1 year later after the last comment and still not working on |
The discussion has been moved to #9430. ESM support is being introduced in Jest 27, so even if today was "10 years later" it would still not be working on |
Got this issue with |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
I think this could be close now, right @SimenB? |
Yeah, we can probably do that. Support is mostly done on our side, documented at https://jestjs.io/docs/ecmascript-modules. Further work has been blocked for about 15 months in node itself: nodejs/node#37648 |
i m hanging here for the answer |
Do you want to request a feature or report a bug?
I want to request a feature.
What is the current behavior?
Right now Jest does not support test suites with
import
statement. They result in the following error:What is the expected behavior?
Would be great if Jest supported ES modules natively.
Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
Jest:
21.2.1
node:
8.9.0
npm:
5.5.1
Before, native support of ES modules was not possible since node.js did not support them. Starting from a few versions ago, node.js added support of ES modules with a flag (https://nodejs.org/api/esm.html). It would be absolutely great if Jest would match this with adding support of ES modules too, probably with a flag or even without it.
Node.js requires ES modules to have an
.mjs
extension. In order to support ES modules Jest needs to add a recognition of those extensions. Jest will also need to pass an--experimental-modules
flag to node.js until node will implement support of modules without a flag. I'm not sure if any other required changes would be needed within Jest in order to support this. I can only hope it will not be terribly hard.Ideally, it would be cool if Jest would recognize modules even in files without
.mjs
extensions since code that targets browsers do not use them, but I don't know if it is ever possible. Node.js provides loader hooks for that (https://nodejs.org/api/esm.html) but this still doesn't solve the issue with a reliable determination of what type of module the file is.I believe ES modules are a great feature vastly superior to all existing JS module solutions. Having it implemented in node.js opens a door for Jest to have it too. This would allow developers stick to using the first truly standardized JS module format not only throughout development, but trough testing as well.
The text was updated successfully, but these errors were encountered: