Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"unexpected token import" error while running tests

when I run the test, I get the following error:

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React from 'react';
                                                                                         ^^^^^^

SyntaxError: Unexpected token import

It makes sense, since all the lines of "import React" or "import Enzyme", are marked as an error by ESlint. I don´t know why.

This is my .babelrc file:

{
  "presets": [
    "react",
    "stage-2",
    ["env", {
      "test": {
        "presets": ["env", "react", "stage-2"],
        "plugins": ["transform-export-extensions"],
        "only": [
          "./**/*.js",
          "node_modules/jest-runtime"
        ]
      },
      "targets": {
        "browsers": ["last 2 versions", "safari >= 7"]
      },
      "modules": false
    }]
  ]
}
like image 867
Iván Avatar asked Mar 16 '18 19:03

Iván


1 Answers

If you are using babel 6 and jest 24 then be informed that jest 24 has dropped support for babel 6.

There are two solutions

  1. Upgrade your app to babel 7. (actively maintained by its developers)
  2. If you don't want to upgrade, make sure that you stick on jest 23.

There is one more work around if you want to use jest 24. Use babel-jest locked at version 23.

"dependencies": {
  "babel-core": "^6.26.3",
  "babel-jest": "^23.6.0",
  "babel-preset-env": "^1.7.0",
  "jest": "^24.0.0"
}
like image 199
Santosh Avatar answered Oct 03 '22 00:10

Santosh