ts-jest does not recognize es6 imports

丶灬走出姿态 提交于 2019-12-03 04:50:00

I eventually found out what the problem was. It turns out it was there in ts-jest's README all the time.

There's a section in the README titled Using ES2015+ features in Javascript files. In these cases, you need to instruct jest to use babel-jest as a transform for .js files.

"jest": {
  "transform": {
    "^.+\\.jsx?$": "babel-jest", // Adding this line solved the issue
    "^.+\\.tsx?$": "ts-jest"
  },
  // ...
},

As the documentation you found says, ts-jest requires CommonJS modules, so since your main tsconfig.json sets "module": "es6", you'll need to create a separate tsconfig.json file for ts-jest that extends your main tsconfig.json and sets "module": "commonjs".

If you are using create-react-app , try this :

Even though create-react-app comes pre-configured, we still had to add in custom configuration since our app wasn't initially built with cra. I installed some new dev dependencies:

"babel-cli": "^6.26.0",
"babel-jest": "^22.4.1",
"babel-preset-react-app": "^3.1.1",

And also updated the .babelrc (create this file if it doesn't exist ) :

{
"presets": [
    "@babel/preset-env",
    "@babel/preset-react"
      ]
}

And now both jest and npm test both work as they should.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!