I'm using Jest to test my React app.
Recently, I added DeckGL to my app. My tests fail with this error:
Test suite failed to run /my_project/node_modules/ export {default as DeckGL} from './deckgl'; ^^^^^^ SyntaxError: Unexpected token export at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:318:17) at Object.<anonymous> (node_modules/) at Object.<anonymous> (node_modules/) This looks like an issue with Jest transforming a node module before running it's tests.
Here is my .babelrc:
{ "presets": ["react", "es2015", "stage-1"] } Here is my jest setup:
"jest": { "testURL": "", "setupFiles": [ "./test/jestsetup.js" ], "snapshotSerializers": [ "<rootDir>/node_modules/enzyme-to-json/serializer" ], "moduleDirectories": [ "node_modules", "/src" ], "moduleNameMapper": { "\\.(css|scss)$": "<rootDir>/test/EmptyModule.js" } }, I seem to have the correct things necessary to transform export {default as DeckGL }. So any ideas whats going wrong?
6 Answers
This means, that a file is not transformed through TypeScript compiler, e.g. because it is a JS file with TS syntax, or it is published to npm as uncompiled source files. Here's what you can do.
Adjust your transformIgnorePatterns allowed list:
{ "jest": { "transformIgnorePatterns": [ "node_modules/(?!@ngrx|(?!deck.gl)|ng-dynamic)" ] } }By default Jest doesn't transform node_modules, because they should be valid JavaScript files. However, it happens that library authors assume that you'll compile their sources. So you have to tell this to Jest explicitly. Above snippet means that @ngrx, deck and ng-dynamic will be transforemed, even though they're node_modules.
5And if you are using 'create-react-app', it won't allow you to specify 'transformIgnorePatterns' via Jest property in package.json
You can use CLI as below in your package.json to override and it works :
"scripts": { "test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!your-module-name)/\"", }, 1This is because Node.js cannot handle ES6 modules.
You should transform your modules to CommonJS therefore.
Babel 7 >=
Install npm install --save-dev @babel/plugin-transform-modules-commonjs
And to use only for test cases add to .babelrc, Jest automatically gives NODE_ENV=test global variable.
"env": { "test": { "plugins": ["@babel/plugin-transform-modules-commonjs"] } } Babel 6 >=
npm install --save-dev babel-plugin-transform-es2015-modules-commonjs
to .babelrc
"env": { "test": { "plugins": ["transform-es2015-modules-commonjs"] } } 4Jest by default won't compile files in the node_modules directory.
transformIgnorePatterns [array]
Default: ["/node_modules/"]
An array of regexp pattern strings that are matched against all source file paths before transformation. If the test path matches any of the patterns, it will not be transformed.Default: ["/node_modules/"]
DeckGL seems to be in ES6, to make jest able to read it, you need to compile this as well.
To do that, just add an exception for DeckGL in the transformignorePatterns
"transformIgnorePatterns": ["/node_modules/(?!deck\.gl)"] I was having this issue with a monorepo. A package in the root node_modules was breaking my tests. I fixed by changing my local .babelrc file to babel.config.js. Explanation:
This code worked for me // .babelrc
{ "presets": [ ["env", { "modules": "commonjs", // <- Check and see if you have this line "targets": { "browsers": ["> 1%", "last 2 versions", "not ie <= 8"] } }], "stage-2" ], "plugins": ["transform-vue-jsx", "transform-runtime"], "env": { "test": { "presets": ["env", "stage-2"], "plugins": ["transform-vue-jsx", "transform-es2015-modules-commonjs", "dynamic-import-node"] } } } jest understands commonJs so it needs babel to transform the code for it before use. Also jest uses caching when running code. So make sure you run jest --clearCache before running jest.
Tested Environment:
Node v8.13.0
Babel v6+
Jest v27