Intellisense for Jest not working in VS code
Asked Answered
T

13

92

Edit: Run npm install @types/jest --save-dev To fix

Just trying to type it() and the auto suggestion is isTag

enter image description here

I've tried adding a jsconfig.json

{
"compilerOptions": {
    "target": "es6"
},
"exclude": [
    "node_modules",
    "assets"
  ]
}

Thank you for anyone who has a suggestion for this!!

Tarsuss answered 10/9, 2019 at 15:38 Comment(1)
M
178

Add to your jsconfig.json:

{
    "typeAcquisition": {
        "include": [
            "jest"
        ]
    }
}

If this do not work try with this command:

npm install @types/jest

or

yarn add -D @types/jest
Myramyrah answered 10/9, 2019 at 16:19 Comment(11)
Thanks for the suggestion, but still getting the same results.Tarsuss
Do you have jest npm package installed?Myramyrah
Updated my answer, give it a try please.Myramyrah
That did the trick, thank you!! would you recommend globally installing this?Tarsuss
Nice! As you wish, you can install globally or have it installed as a dev dependency.Myramyrah
@types/jest shows jest not found when run npm run startExecute
You first have to install jest locally or globally: npm i jest. Assuming you have a project of two directories like backend and frontend put this file in them both. Then restart vscode. Autocomplete should be working now.Imide
This was the magic npm install @types/jestFeudal
Installing @types/jest worked.Sevenfold
I CONFIRM that we need to restart VSCODE after install @types/jest and it works!Preussen
Please clarify where can I find "jsconfig.json", I have package.json file onlyThun
A
72

Update 2024

Just install @types/jest via this command:

npm i @types/jest --save-dev

or

yarn add -D @types/jest
Anoa answered 20/9, 2020 at 17:20 Comment(4)
i would update this to say 'npm i @types/jest --save-dev' this should not be exported with the project as a normal dependency and is only for local testingHeadlight
this worked. dont rob npm like that, we have -D option too instead of verbose --save-dev hahaOddball
Yes, but --save-dev is more meaningful and suitable for tutorial aspects.Anoa
I added a small edit to the top of this question in the context of SOLUTION:Tarsuss
L
24
{
    "typeAcquisition": {
        "include": [
            "jest"
        ]
    }
}

for those who have no luck adding above to jsconfig.json in the root folder: try adding it to tests folder (the folder containing *.test.js files)

Lilianaliliane answered 15/5, 2020 at 5:10 Comment(4)
Thank you. Put this inside test folder (not the root one) is working for me.Hyperbolism
why the same thing works for some, and not for others?Execute
So far, none of these answers works for me. Installed types and tried this config folder in both the root and the tests folder. Zero progress.Kelleykelli
This worked, I think it's because I am changing roots in my jest.config.js, so I had to putjsconfig.json inside my root folder.Diameter
C
5

This won't be for everyone but if the codebase has multiple packages in a monorepo and you use vscode as your editor, you might want to install @types/jest in the root, rather in the individually packages.

Carafe answered 12/12, 2021 at 7:30 Comment(1)
Thank you for this. I had the intuition but did not try, saw this and it worked.Frech
D
3

The Typescript extension was the root cause of my issue. I was trying to use Jest, but the expect type was being overridden by the expect from expect.js, which was apparently being auto-installed into a global cache by Typescript at /Users/cameronhudson/Library/Caches/typescript/5.0/node_modules/@types/expect.js. My preliminary "fix" is to disable automatic type acquisition by the Typescript extension. This can be done in the settings for the extension.

enter image description here

I have a feeling that this is a heavy-handed solution, but hopefully it will lead someone to a proper solution.

Dex answered 3/5, 2023 at 19:50 Comment(0)
M
2
npm i -D @types/jest

it adds a line automaticaly in package.json :

    "devDependencies": {
        "@types/jest": "^29.5.2",
        "jest": "^29.5.0"
      }

You have nothing else to do.

Might answered 30/6, 2023 at 12:32 Comment(2)
You are a life saverParent
You are my life saver!Relic
M
1

Non of the solution works for me. After diving into some github repos in which I found it working, I found a file called jest.config.js. Following are the configs of the file:

module.exports = {
  transform: {
    '^.+\\.ts?$': 'ts-jest',
  },
  testEnvironment: 'node',
  testRegex: './src/.*\\.(test|spec)?\\.(ts|ts)$',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  roots: ['<rootDir>/src'],
};

Note: This is for ts. Change all the ts with js if you are not using typescript. After saving, Restart Vscode and it will work as expected.

Montenegro answered 4/10, 2021 at 21:21 Comment(0)
S
0

Where you call it makes a difference. Sometimes intellisense doesn't work when you are in a bad function or code block.

So if you try:

  beforeEach(() => ({
    mockedUseAccount.
  }));

Intellisense doesn't work.

If you try:

  beforeEach(() => {
    mockedUseAccount.
  });

Intellisense works fine.

Sillsby answered 29/4, 2022 at 14:50 Comment(0)
A
0

Strange that these haven't been shared - Use jsdoc instead:

https://jestjs.io/docs/configuration

jest.config.js

/** @type {import('jest').Config} */
const config = {
  verbose: true,
};

module.exports = config;

/** @returns {Promise<import('jest').Config>} */
module.exports = async () => {
  return {
    verbose: true,
  };
};

If you're using a TypeScript file (jest.config.ts):

import type {Config} from 'jest';

const config: Config = {
  verbose: true,
};

export default config;
import type {Config} from 'jest';

export default async (): Promise<Config> => {
  return {
    verbose: true,
  };
};
Agc answered 30/11, 2022 at 20:40 Comment(0)
P
0

The correct answer is indeed to install @types/jest:

yarn add -D @types/jest

However, if you have that installed and Jest's functions (describe, it, etc.) still have no type information in VS Code, check your your typeRoots in tsconfig.json.

I had made some custom types for a package that didn't have any and placed them in a ./types folder. To get TypeScript to recognise those, I added typeRoots: ["./types"] to my tsconfig.json. However, specifying type roots there overrides TypeScript's default value of ["./node_modules/@types"], thus it will no longer be able to find @types/jest.

So, if you have a custom value for typeRoots in your tsconfig.json, then besides installing @types/jest, make sure you also include this (replace ./types with any other custom type root folders you have configured):

    "typeRoots": ["./node_modules/@types", "./types"],
Percheron answered 24/4, 2023 at 11:2 Comment(0)
G
0

If nothing has been worked so far, you can try this.

check "types": ["node","jest"], in tsconfig.json file.

If jest is not added in the list, add and restart VS Code.

Gaytan answered 31/5, 2023 at 3:35 Comment(0)
L
0

I didn't see this mentioned before but using explicit imports are a valid option if the dependencies are already in the package.json and vscode is still not getting the message. In my case vscode kept intellisensing mocha for describe, it, beforeAll, and expect.

I tried all options but creating a jsconfig.json

S/o @vikramvi for his comment on OP's question which led me to that solution.

Before explicit imports Before explicit imports 1 Before explicit imports 2

After explicit imports After explicit imports

Landsturm answered 14/2 at 16:23 Comment(0)
A
0

I had to run tsc --init to generate a tsconfig.json file. After that, VSCode picked up the types.

Amplexicaul answered 7/3 at 18:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.