Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create-react-app jest encountered unexpected token {

I'm developing a React app with a QR-scanner in it with create-react-app.
I've added the module react-qr-reader which in turn uses the modules webrtc-adapter.

It all works great, until I run yarn test. Then it shows me this error: enter image description here

I've already ejected the project so I can use transformIgnorePatterns and added node_modules/webrtc-adapter to the array, but that still results in the same error.

Can anyone help me with this?

like image 917
CherryNerd Avatar asked Apr 03 '19 10:04

CherryNerd


2 Answers

If you don't want to eject from Create-React-App you can use the CLI in your package.json to override 'transformIgnorePatterns'.

Reference - https://github.com/facebook/create-react-app/issues/2537#issuecomment-390341713

  "scripts": {
    "test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!your-module-name)/\"",
  },
like image 142
JP4 Avatar answered Oct 22 '22 21:10

JP4


It's because of de ES6 syntax in a package into node_modules, you need to config the "transformIgnorePatterns" to transform this package.

The issue on Jest: https://github.com/facebook/jest/issues/2081

How to configure: http://facebook.github.io/jest/docs/tutorial-react-native.html#transformignorepatterns-customization

"transformIgnorePatterns": [
  "node_modules/(?!(react-qr-reader)/)"
]

if this doesn't work, use the babel.config.js with this configuration (is important the file ".js", this configuration don't work in ".babelrc" file):

module.exports = {
  presets: [
    [
      "@babel/preset-env",
      {
        "modules": "commonjs",
        "debug": false
      }
    ],
    /// your presets
  ],
  plugins: [
    //... your plugins
  ]
};
like image 6
Janderson Constantino Avatar answered Oct 22 '22 22:10

Janderson Constantino