After setting up Jest in React + TypeScript, I encountered an error when trying to run a test using the command npm test
. The error message displayed was:
Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.
I have already installed Jest types and removed types from tsconfig.json
, but the error persists.
{
"compilerOptions": {
"target": "es6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"plugins": [{ "name": "typescript-tslint-plugin" }],
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
"pretty": true,
"baseUrl": "src",
"types": ["jest"],
"typeRoots": ["./src/types"],
"suppressImplicitAnyIndexErrors": true
},
"include": ["src", "node_modules/@types/jest"],
"exclude": ["node_modules"]
}
Package.json
"jest": {
"transform": {
".(ts|tsx)": "ts-jest"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
]
},
"devDependencies": {
"@babel/plugin-proposal-export-default-from": "^7.2.0",
"@types/enzyme": "^3.9.3",
"@types/jest": "^24.0.14",
...
"tslint-react-hooks": "^2.1.0"
}