-
-
Notifications
You must be signed in to change notification settings - Fork 536
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
Jest: SyntaxError: Unexpected token 'export' on "@bundled-es-modules/js-levenshtein" #1810
Comments
I got the same issue. After some troubleshooting, I found that it's more to do with the jest-runner and esm to cjs transformation than mock service worker. The problem Now, jest-runner looks for files to load and parse by using the "main" property of each of the package.json. This is all taken from the modules that are involved in a test. After jest-runner found all of the files involved in a test run, it loads their code and tries to parse it with node:vm.Script. And this is the point where it fails, once the Script class tries to parse "index-esm.js" code, it fails with a SyntaxError as soon as it hits an EcmaScript construct, in this case "export" keyword. Since, it expects a commonjs (cjs) syntax, and it received an ESM one. Quick fix Example for "transformIgnorePatterns": [
"[/\\\\]node_modules[/\\\\].+[^esm]\\.(js|jsx|mjs|cjs|ts|tsx)$",
"^.+\\.module\\.(css|sass|scss)$"
], Note, I haven't had time to test it properly, I quickly hacked it just to get it working, but this should be a clean quick fix if the regex pattern is done correctly. A bit bigger problem Going forward NOTE: I'm running node 18, jest 27, react-scripts 5.0.1, and msw 2.0.0. |
Thanks for reporting this. Jest is historically bad with ESM and I don't believe they support it properly yet. If you can, consider migrating to modern test frameworks, like Vitest. There are no module issues there, as long as a number of other issues you may find with Jest. The same goes for Create React App. The project is officially dead, and the React team recommends using a production-grade framework like Next.js or Remix instead. Again, migrate at your own consideration and possibilities. If migrating is not an option for you, follow @pord911's advice and configure the |
Thank you both for the quick responses! I don't use CRA, I just used it to create an example. @kettanaito I understand your frustration with jest, but I don't think most teams are going to migrate test frameworks when they run into an issue upgrading a library. @pord911 thanks for the suggestion! For anyone else coming across this issue, here is what was needed to make this work: In my jest config, I had to change the preset so it would transform ESM to commonjs. By default preset: "ts-jest/presets/js-with-babel",
transformIgnorePatterns: ["/node_modules/(?!(@bundled-es-modules)/)"], Useful jest docs: |
They will migrate sooner or later. I'd rather MSW incentivizes them to do that sooner. Jest doesn't support ESM, and by the looks of it, never will. MSW has been historically helpful in highlighting problematics application areas by relying on standards. This is yet another example of it. Anything that makes Jest happy with proper export conditions and ESM works. |
Hi everybody //jsdom-extended.js class JSDOMEnvironmentExtended extends JSDOMEnvironment {
} module.exports = JSDOMEnvironmentExtended; //.babelrc //package.json |
Prerequisites
Environment check
msw
versionBrowsers
Chromium (Chrome, Brave, etc.)
Reproduction repository
https://github.com/RobinClowers/msw-jest-esm-error
Reproduction steps
npm i && npx jest
Current behavior
It appears that msw is importing an ESM package, which seem to fail unless my package is configured to be an ESM.
Expected behavior
The test should run successfully.
The text was updated successfully, but these errors were encountered: