"Cannot find name 'jest'" Typescript error

A little bit green with Webstorm. Can't solve this problem with typescript compiler service flagging an error for TS2304: Cannot find name 'jest'.  Do I need to change my run configuration for jest?

My tests still run as expected with no problems, but the intellisense is flagging some jest keywords.  Any suggestions?

13 comments
Comment actions Permalink

Try adding jest typings to your project (npm i @types/jest) - does it help? Typings added via Settings | Languages & Frameworks | JavaScript | Libraries are not available to compiler, you need to install them to the project folder to get the project compiled

0
Comment actions Permalink

I have added jest typings to my project. here is my `devDependencies` within package.json:

I do not have this problem in VS Code, so I was suspicious that there may be a setting within WebStorm that I was missing?

0
Comment actions Permalink

works fine for me using exactly same @types/jest version

how many tsconfig.*.json files do you have in your project? what do they look like? Please share a project I can use to recreate the issue

0
Comment actions Permalink

bingo. this was it. I have a couple of different tsconfig.*.json files in my project. adding "jest" to "types" within one of them solved the problem. thank you.

3
Comment actions Permalink

I had same error message and it turned out to be because I was using the latest version of the jest library but @types/jest has not yet been released with the same version number. Downgrading to 24.0.0 for both solved my problem. Incidentally, jest-preset-angular 7 does requires jest 24.1 as peer dependency and version 6 works only on jest 23. But so far I've just ignored the warning.

0
Comment actions Permalink

Hi
My solution is:

  1. create file tsconfig.json
  2. add this code:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"types": [
"mocha",
"jest"
]
},
"exclude": [
"node_modules", "dist"]
}

I added to the types - "jest"

It work for me

 

2
Comment actions Permalink

If you have attached multiple projects, then all TS projects should have in tsconfig the "jest" in types.

That means that the Jest tests are affected by the ts config of other projects! That's not expected.

This occurs in WebStorm 2020.3.1
Build #WS-203.6682.155, built on December 28, 2020

0
Comment actions Permalink

>That's not expected.

That's expected unless your attached projects have a common root dir with tsconfig.json in it.

tsconfig.json should be located in the project root folder, the compiler traverses the subdirectories recursively looking for .ts files. Those files that are located outside of this folders structure are not a part of current Typescript project, so the settings in your tsconfig don't affect it.

You can see what files are actually included by running tsc -p <path to your tsconfig.json> --listFiles to see the list

0
Comment actions Permalink

I'm still getting the same error, even after adding:

"types": ["jest"]

to all (two) tsconfig files.  Any other suggestions?

0
Comment actions Permalink

Did you add jest typings to your project (npm i @types/jest)?

0
Comment actions Permalink

Yes, did that.

0
Comment actions Permalink

please share a project the issue can be repeated with (you can remove all source files from it, keeping just a pair of dummy test files, it's the structure/configuration files/package.json files that matter)

0
Comment actions Permalink

I suppose your project has one tsconfig file with excluded spec files. You need to split them

0

Please sign in to leave a comment.