Cannot find name 'describe'. Do you need to install type definitions for a test runner?
I'm using VSCode as my IDE and in my Angular project, I had to comment-out/remove types in tsconfig.json and add jest in types at tsconfig.spec.json.
tsconfig.json
{
"compilerOptions": {
// "types": []
}
}
tsconfig.spec.json
{
"compilerOptions": {
"types": ["jest", "node"]
}
}
It's a bit tricky one because both: your IDE (i.e. VSCode) and TypeScript use tsconfig.json for their own purpose.
Simple checklist to solve the initial problem:
(for TypeScript and Jest)
- Make sure you have
@types/jest
and@types/node
installed. - Make sure you have linked these types in
tsconfig.json
so that:types: ["jest", "node"]
- Make sure you don't have your tests or the tests directory excluded from
tsconfig.json
configuration inexcluded
property.
Side-effect on transpilation
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 of the cases you will have either:
- separate
tsconfig.prod.json
with configuration that overwrites the default one. There are many settings likeinlineSource
,sourceMaps
,inlineSourceMaps
which you'd probably want to disable too and then usetsc --project tsconfig.prod.json
to build - terminal (npm/yarn script) command that overwrites the default config with the specific flags. Example:
npx 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. Might be less complex than overwriting the configuration and easier to maintain as a build step. In such case you may use the command: yarn rimraf build/**/*.test.js
None of the above fixed my issue.
I had to add "@types/jest"
to the types
array in the tsconfig.json
file.
This worked for me:
import '@types/jest';