9

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
    }]
  ]
}
skyboyer
  • 22,209
  • 7
  • 57
  • 64
Iván
  • 401
  • 2
  • 6
  • 17

2 Answers2

3

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"
}
Kamal Panhwar
  • 2,345
  • 3
  • 21
  • 37
Santosh
  • 116
  • 1
  • 8
0

Hello dev can you try,

install babel-jest

set this inside .babelrc

  "env": {
    "test": {
    "presets": ["es2015", "react"],
    "plugins": ["syntax-object-rest-spread", "transform-es2015-modules-commonjs"]      
  },

testing with jest

Angel
  • 1,670
  • 1
  • 11
  • 14