Closed panitaxx closed 4 years ago
Make sure your package.json
contains an entry like "@types/jest": "^24.0.15",
, reinstall NPM and use 'Typescript: Restart TS Server' option in VSCode. (CMD+SHIFT+P)
And if it still does not work?
Try restarting. This sometimes help.
I also experience VSCode not recognizing types every once in a while though they are definitely installed. Sometimes restarting helps. Sometimes it doesn't. I wish I knew how to reproduce it. This clearly has been an issue for some while.
Actually it somehow magically started working after a while... Thanks :)
@mkolodziejczyk-branch nice to hear!
The only way I was able to fix this, was by adding the tests/
folder to "include" in the tsconfig.json
file:
"include": [
"src/**/*.ts",
"tests/*.ts"
]
I am having a similar issue. VSC recognises jest types if the project is opened from project root where tsconfig.json is located. If the project is opened from its parent directory, only way to get VSC to recognise the jest types is to include tests in tsconfig.json. Unfortunately this causes the tests to be compiled as well, which is often undesirable.
It would be great if VSC could recognise jest types even if opened in monorepo style.
@JelsB , I also included tests/**/*.ts
but this change the build. Indeed, dist
dir contains src
and test
folder.
Does anyone has a fix for having tests files taking in account in VScode wihout addint them in include
array?
Currently, I got this VSCode error :
Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try
npm i @types/jest
ornpm i @types/mocha
.ts(2582)
For the monorepo issue, try this:
└── package
├── __tests__
│ ├── demo.test.ts
│ └── tsconfig.json
├── src
│ └── demo.ts
└── tsconfig.json
Where package/tsconfig.json
has:
{
// ...
"include": [ "src/**/*.ts" ]
}
And package/__tests__/tsconfig.json
has:
{
"extends": "../tsconfig.json",
"src": "**/*.ts"
}
Running tsc
from the package/
folder will not build __tests__
. VSC will still pick up @types/jest
in the __tests__
folder.
I am having a similar issue. VSC recognises jest types if the project is opened from project root where tsconfig.json is located. If the project is opened from its parent directory, only way to get VSC to recognise the jest types is to include tests in tsconfig.json. Unfortunately this causes the tests to be compiled as well, which is often undesirable.
Thank you! This was driving me nuts until I read your post.
I had the following section in my tsconfig.json file, and commenting it out fixed the issue...
"exclude": [
"node_modules",
"**/mocks/*.ts",
"**/*.spec.ts",
"**/*.test.ts",
"**/*.spec.tsx",
"**/*.test.tsx"
],
This may be old, but I figure adding my details may be useful to anyone coming around looking for an answer.
TLDR; Open the project folder directly, instead of opening a mono-repo's root directory
I noticed that VSCode does not find the jest globals when a parent folder of a project is your current "root". Example below is the structure I had, and while editing test1.test.ts the gobals test and expect are not found by VSCode even though @types/jest is installed.
parent-folder
If I open a new VSCode instance and choose to open the package folder as the root, VSCode does find the jest globals and all looks and works great.
Hope this helps someone 👍
@codingLogan Thanks, that solves the problem for me, but now how do you work on files in another branch off the root?
@codingLogan Thanks, that solves the problem for me, but now how do you work on files in another branch off the root?
Hey @jonahx , in my particular case I opened up a few instances of VS Code rather than having just one of the whole monorepo. One was opened up with package as the root, and one with test-client.
I have installed this package but VScode complains that Jest methods do not exist (describe, it, expect). Any way to fix this?