Support Testing with imports of JS files in ESM
See original GitHub issueCurrent Behavior
When running test files with that contain ES6 module imports with Jest, I get the following error:
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Is there an existing recommendation for handling ES6/ESNext module imports, if so, could we add documentation around this? If it doesn’t exist, could we add recommendation on how to handle it? I’m happy to help if I can. Could this be a TSDX feature, to switch on and off?
Desired Behavior
Either handled by default by TSDX, or there is clear recommendations on how best to handle this.
Suggested Solution
Happy to add docs with the recommendation to resolve, although I’m not sure what the recommendations are as yet.
Who does this impact? Who is this for?
Users importing other packages that use ES6.
Describe alternatives you’ve considered
I considered using babel-jest and configuring it myself, but this seems to make TSDX a little redundant.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:21 (8 by maintainers)
Top Results From Across the Web
What does it take to support Node.js ESM? – The Guild
There is a relatively small but effective way of automated testing for all the top-level imports in ESM, you can have an ESM...
Read more >Getting Started with (and Surviving) Node.js ESM
1 - Resolving a simple import like "colors" to a file like "index.js" ... In May, 2020, Node.js v12.17.0 made ESM support available...
Read more >ECMAScript Modules - Jest
Since ESM evaluates static import statements before looking at the code, the hoisting of jest.mock calls that happens in CJS won't work for...
Read more >Jest and ESM throwing errors with import fs - Stack Overflow
Jest ships with experimental support for ECMAScript Modules (ESM). It is experimental and has many bugs. You need to transpile your ESM ......
Read more >Using ES Modules (ESM) in Node.js: A Practical Guide (Part 3)
Mocha was actually the first test runner to support ESM (support written by yours truly ). So write your test files using ESM...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I don’t believe this is fixed yet and this should be reopened.
Using the latest
tsdx@0.14.1
, Jest tests fail when they import something in ESM (eg.p-map
) fromnode_modules
, also when using theNODE_OPTIONS=--experimental-vm-modules
option as described in the docs:Repo: https://github.com/karlhorky/tsdx-test-esm-fail Repl.it (just hit the Run button at the top): https://replit.com/@karlhorky/tsdx-test-esm-fail
Hi,
Any solution to this problem?
I’m going through the same situation.
I have a third-party file in my project that internally imports two libs in js and when I run the test I get this error:
Jest encountered an unexpected token
I solved it using @karlhorky suggestion documented in pr #1019, but I would like to know if this problem will be solved.