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)

Notes:
{
"compilerOptions": {
"types": ["jest", "node"]
},
"types": ["jest"],
"typeRoots": ["./src/types", "./node_modules/@types"],
}
module.exports = {
preset: 'ts-jest',
roots: ['<rootDir>/src'],
testEnvironment: 'node',
moduleFileExtensions: ['ts', 'js', 'json', 'node'],
};
{
"presets": [
["@babel/preset-env", {"targets": {"node": "current"}}],
"@babel/preset-typescript"
],
"plugins": ["@babel/transform-runtime"]
}

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
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"]
}
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
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