ts-jest does not recognize es6 imports
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.