Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 455
ProvingBard Avatar asked Mar 14 '26 10:03

ProvingBard


2 Answers

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"]
}
like image 134
mithdenpaf Avatar answered Mar 16 '26 00:03

mithdenpaf


If someone is facing a similar issue, please 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 by default points to ./node_modules/@types and types includes jest

like image 39
Sushil Kumar Avatar answered Mar 15 '26 22:03

Sushil Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!