How to run jest when using es6 packages in node_modules
See original GitHub issueIs this a bug report?
Not sure. More of a configuration issue I guess.
TLDR
Trying out the feature described in #1125 which should allow the usage of libraries distributed as es6 modules in an app create with create-react-app, the app runs fine but the Jest test suite will not.
How to reproduce
- create a new create-react-app application
npx create-react-app jest-es6-modules
- in the new application,
yarn add lodash-es
- modify
src/App.js
adding any named import, such asimport { isEmpty } from 'lodash-es'
- run
yarn start
(everything works) - run
yarn test
Here’s the Jest error, related to Jest not babel-transforming the modules in es6 format:
● Test suite failed to run
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.
[...]
Details:
/Users/nerfologist/test/jest-es6-modules/node_modules/lodash-es/lodash.js:10
export { default as add } from './add.js';
^^^^^^
SyntaxError: Unexpected token export
1 | import React, {Component} from 'react';
2 | import logo from './logo.svg';
> 3 | import {isEmpty} from 'lodash-es';
| ^
4 | import './App.css';
5 |
6 | class App extends Component {
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
at Object.<anonymous> (src/App.js:3:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.279s
Ran all test suites related to changed files.
Question
What is the best/recommended way to have Jest transform one or more of these node_modules
?
I’m thinking of adding a Jest configuration containing the transformIgnorePatterns
directive.
Thanks in advance!
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (4 by maintainers)
Top Results From Across the Web
How to setup jest with node_modules that use es6
Option 1.) Setup your babel configuration to handle ES6 imports by add a testing env option (the testing environment flag will be defined...
Read more >ECMAScript Modules - Jest
If you use Yarn, you can use yarn node --experimental-vm-modules $(yarn bin jest) . This command will also work if you use Yarn...
Read more >How to setup jest with node_modules that use es6-Reactjs
Option 1.) Setup your babel configuration to handle ES6 imports by add a testing env option (the testing environment flag will be defined...
Read more >How to Configure Jest to Test React and ES6
npm install --save-dev jest-cli babel-jest react-addons-test-utils ... With that done, we're going to modify our package.json file so that it knows to call ......
Read more >Anyone gotten unit tests working with ES6 modules?
Oh wow, this got me the closest so far. I had to change my jest.config.js to use export default { since module.exports =...
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
@Timer
Lodash shipped
lodash-es
, andlodash-amd
, before the wholejsnext
andmodule
fields were a thing. It was shipping before Babel was a thing 🙃The primary
lodash
package is already stuffed pretty full, clocking in at a bit over 4MB, because it has core builds, monolithic builds, minified builds, fp modules, etc. Being thatlodash
is the most depended upon package impacting over 162,495 packages its package size impacts users, so I don’t want to add any more to it. The plan for the v5 release is to cut all the fat to ship a < 100kB primary package written in ESM and powered by theesm
loader.I see dual packages as a bit of an anti-pattern. They inflate your production package for a possible build/bundle-time optimization and have presented problems for the emerging, but still very much WIP, Node
--experimental-modules
when mixing CJS and ESM. I’ve been working on theesm
loader, for the last 1.5 years, so folks can ship ESM-only package that work in all supported Node versions (helping folks transition from CJS to ESM).The single-function Lodash packages are soft-deprecated. It turns out trying to manage 400+ packages doesn’t work out so well. They also lead to bigger bundles than simply using
lodash
orlodash-es
and cherry-picking the modules from them.@nerfologist
Jest supports specifying source
transform
andtransformIgnorePatterns
in its config files and defaults to using thebabel-jest
transform. In its next releaseesm
will support being used as a Jest transform so you could use that was well. Something like:I have the same issue with “react-redux”.
The temporary fix for now seems to be to specify “transformIgnorePatterns” as mentioned above: