Jest setup "SyntaxError: Unexpected token export"

ReactjsReduxJestjs

Reactjs Problem Overview


I'm implementing tests into an existing project that currently has no tests. My tests are failing to compile node_modules/ imports.

/Users/me/myproject/node_modules/lodash-es/lodash.js:10
export { default as add } from './add.js';
^^^^^^
SyntaxError: Unexpected token export
  
  at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:320:12)
  at Object.<anonymous> (app/reducers/kind_reducer.js:2:43)
  at Object.<anonymous> (app/reducers/index.js:12:47)

The workaround I've found is to 'whitelist' node_modules in package.json jest config like this:

"jest": {
    "transformIgnorePatterns": [
      "!node_modules/"
    ]
  }

This seems like a hack because it takes over 1 minute to run a simple test that imports node_modules/lodash-es/lodash.js.

Reactjs Solutions


Solution 1 - Reactjs

If none of the other solutions worked for you, you can try this in your jest

"moduleNameMapper": {
	"^lodash-es$": "lodash"
}

It will replace lodash-es with the commonjs version during testing runtime.

Solution 2 - Reactjs

I had to add this into my .jestconfig:

"transformIgnorePatterns": [
  "<rootDir>/node_modules/(?!lodash-es)"
]

Solution 3 - Reactjs

Posting a more complete answer here:

Jest by default does not transform node_modules because node_modules is huge. Most node modules are packaged to expose ES5 code because this is runnable without any further transformation (and largely backwards compatible).

In your case, lodash-es specifically exposes ES modules, which will need to be built by Jest via babel.

You can try narrowing your whitelist down so Jest doesn't try to pass every JavaScript file within node_modules through babel.

I think the correct configuration in your case is:

"jest": {
  "transformIgnorePatterns": [
    "/!node_modules\\/lodash-es/"
  ]
}

Solution 4 - Reactjs

For create-react-app users who are looking for a fix, here's what worked for me:

// package.json
...
  "jest": {
    "transformIgnorePatterns": [
      "<rootDir>/node_modules/(?!lodash-es)"
    ]
  },
...

Overriding options in jest.config.js file didn't work for me. Keep in mind that not every option can be overridden, here's a list of supported options: https://create-react-app.dev/docs/running-tests#configuration

Solution 5 - Reactjs

Probably someone finds this useful:

In my case, I have an Angular application that uses lodash-es package. During the testing, I am having the same error as the author.

OPatel's answer worked fine for me with a little tweak (add it to your jest.config.ts):

"moduleNameMapper": {
    "lodash-es": "lodash"
}

After the changes I also needed to add the "esModuleInterop": true into my tsconfig.spec.json within the compilerOptions property to get rid of the TypeError: cloneDeep_1.default is not a function.

UPDATE:

After the solution above all the lodash methods return LodashWrapper instead of actual values e.g.

const clone = cloneDeep(object); // LodashWrapper

To get rid of this issue I used this solution: https://github.com/nrwl/nx/issues/812#issuecomment-787141835

moduleNameMapper: {
    "^lodash-es/(.*)$": "<rootDir>/node_modules/lodash/$1",
}

Solution 6 - Reactjs

Renaming .babelrc to babel.config.js and adding transformIgnorePatterns worked for me.

module.exports = {
  "presets": ["@babel/preset-env"]
}

P.S. My jest version is: "jest": "24.9.0"

https://github.com/facebook/jest/issues/6229#issuecomment-419885857

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCory RobinsonView Question on Stackoverflow
Solution 1 - ReactjsOzair PatelView Answer on Stackoverflow
Solution 2 - ReactjsYangshun TayView Answer on Stackoverflow
Solution 3 - ReactjsLewis ChungView Answer on Stackoverflow
Solution 4 - Reactjs8bitjoeyView Answer on Stackoverflow
Solution 5 - ReactjsAndrew KichaView Answer on Stackoverflow
Solution 6 - ReactjsSandeep SharmaView Answer on Stackoverflow