- Install Jest (yarn add -D jest babel-jest @babel/core @babel/preset-env | npm install -D jest babel-jest @babel/core @babel/preset-env)
- Create babel.config.js file in the root of the project with this content
module.exports = { presets: [ ['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-typescript' ] };
- Install type definitions (yarn add -D @types/jest | npm install -D @types/jest)
- Add jest env to package.json
{ ...rest of package.json "jest": { "testEnvironment": "node" } }
- Add jest in esconfig.rc to avoid linting problems. StackOverflow
"env": { "jest/globals": true }
- Modify
npm run test
to run jest.scripts:{ "test": "jest --verbose ./my-directory" }
- We can add "--silent" to avoid displaying console.logs
When I got this error I've solved it creating a "jest.config.js" file in the root of the project with this content:
module.exports = {
roots: [
'<rootDir>/test'
],
testMatch: [
'**/__tests__/**/*.+(ts|tsx|js)',
'**/?(*.)+(spec|test).+(ts|tsx|js)'
],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest'
}
};