'Typescript is not picking up Jest types

The Problem

I am trying to setup a typescript project with jest. However typescript does not seem to pickup the jest types from @types/jest, which is highlighting my keywords giving me the following error:

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'. ts(2582)

enter image description here

Notes:

  • I am using VS Code
  • I have installed @types/jest
  • I have attempted to reinstall all my packages
  • I have reloaded my editor
  • Here is my tsconfig.json
{
  "compilerOptions": {
    "types": ["jest", "node"]
  },
  "types": ["jest"],
  "typeRoots": ["./src/types", "./node_modules/@types"],
}
  • Here is my jest.config.js
module.exports = {
  preset: 'ts-jest',
  roots: ['<rootDir>/src'],
  testEnvironment: 'node',
  moduleFileExtensions: ['ts', 'js', 'json', 'node'],
};
  • Here is my .babelrc
{
  "presets": [
    ["@babel/preset-env", {"targets": {"node": "current"}}],
    "@babel/preset-typescript"
  ],
  "plugins": ["@babel/transform-runtime"]
}
  • Here are the dev dependencies in my package.json

enter image description here

My test runs fine with npm run test, But it is still an annoying issue which I imagine is just a missing ./ in one of my configurations or something. Bit new to typescript/jest though so could be something else. Hopefully I have provided enough info, but am happy to add more if necessary Any help would be appreciated, cheers



Solution 1:[1]

I'm no expert, but I was experiencing the same issue (albeit not the same setup) after I modified the typeRoots property and I was able to get it to work by including "@types/jest" in the "types" property of tsconfig.json

{
"compilerOptions": {
    "target": "es6",                          
    "module": "commonjs",                    
    "outDir": "./dist",                        
    "rootDir": "./",                      
    "strict": true,                           
    "typeRoots": [      
    "./node-modules/@types/",
    "./src/@types/"
    ],                       
    "types": ["jest", "node", "@types/jest"],                          
    "esModuleInterop": true,                  
    "skipLibCheck": true,                    
    "forceConsistentCasingInFileNames": true  
},
"exclude": ["node_modules"],
"include": ["./src/**/*.tsx", "./src/**/*.ts"]
}

Solution 2:[2]

There is a chance, that the dependency tree got corrupted.
Kill node_modules & package-lock.json.
Then run npm i.

Solution 3:[3]

If someone facing the similar issue. Do Check the following fields in your tsconfig

    "include": ["src/**/*"]

make sure you have included the file

    "exclude": ["node_modules", "dist"]

lastly check if you have specified typeRoots which is by defaults points to ./node_modules/@types and types includes jest

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mithdenpaf
Solution 2 avalanche1
Solution 3 kiatng