microsoft / TypeScript-Node-Starter

A reference example for TypeScript and Node with a detailed README describing how to use the two together.
MIT License
11.31k stars 2.77k forks source link

Visual Studio code does not recognize jest ts #196

Closed panitaxx closed 4 years ago

panitaxx commented 5 years ago

I have installed this package but VScode complains that Jest methods do not exist (describe, it, expect). Any way to fix this?

peterblazejewicz commented 5 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)

mkolodziejczyk-branch commented 4 years ago

And if it still does not work?

dschuessler commented 4 years ago

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.

mkolodziejczyk-branch commented 4 years ago

Actually it somehow magically started working after a while... Thanks :)

peterblazejewicz commented 4 years ago

@mkolodziejczyk-branch nice to hear!

JelsB commented 4 years ago

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"
] 
anan44 commented 4 years ago

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.

boillodmanuel commented 3 years ago

@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 or npm i @types/mocha.ts(2582)

WestonThayer commented 3 years ago

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.

dezoito commented 2 years ago

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"
  ],
codingLogan commented 2 years ago

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 👍

jonahx commented 2 years ago

@codingLogan Thanks, that solves the problem for me, but now how do you work on files in another branch off the root?

codingLogan commented 2 years ago

@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.