When using TypeScript in conjunction with Jest, my specs would fail with error messages like:
test/unit/some.spec.ts:1:1 - error TS2582: Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.
test/unit/some.spec.ts:2:3 - error TS2582: 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`.
test/unit/some.spec.ts:3:7 - error TS2304: Cannot find name 'expect'.
test/unit/some.spec.ts:7:1 - error TS2582: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.
The types are already installed.
I use:
"@types/jest": "^23.3.12",
"jest": "^23.6.0",
"ts-jest": "^23.10.5",
"typescript": "^3.1.6"
I run tests using jest --forceExit --coverage --verbose
I'm using Visual Studio Code as my IDE and in my Angular project, and I had to comment-out/remove types in file tsconfig.json and add "jest" in types in tsconfig.spec.json.
{
"compilerOptions": {
// "types": []
}
}
{
"compilerOptions": {
"types": ["jest", "node"]
}
}
It's a bit tricky one because both: your IDE (i.e., Visual Studio Code) and TypeScript use tsconfig.json for their own purposes.
(for TypeScript and Jest)
@types/jest and @types/node installed.tsconfig.json so that: "types": ["jest", "node"]
tsconfig.json configuration in excluded property.If you transpile from TypeScript to JavaScript using tsc or any custom module that relies on tsconfig.json then you may notice that your tests will be transpiled too in such case (you'll see their .js correspondence in your build directory).
However, in most cases you will have either:
tsconfig.prod.json with a configuration that overwrites the default one. There are many settings like inlineSource, sourceMaps, inlineSourceMaps which you'd probably want to disable too and then use tsc --project tsconfig.prod.json to buildnpx tsc --inlineSourceMap false --declarationMap false --inlineSources false --sourceMap false. At that point you can use --excludeFiles or --excludeDirectories flag to exclude your tests from the build as per documentation.The alternative is to use package like rimraf and delete unnecessary files as a part of the build process. It might be less complex than overwriting the configuration and easier to maintain as a build step. In such a case, you may use the command: yarn rimraf build/**/*.test.js.
This worked for me:
import '@types/jest';
None of the previous answers fixed my issue.
I had to add "@types/jest" to the types array in the tsconfig.json file.
The only way I was able to fix this was by adding the tests/ folder to "include" in the tsconfig.json file:
"include": [
"src/**/*.ts",
"tests/*.ts"
]
For those who also have ESLint complaining, you need to add Jest to your .eslintrc.json file:
"env": {
"es2020": true,
"node": true,
"jest": true
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With