6

I'm using VS Code in a Typescript project that uses Jest for testing. For some reason, VS Code thinks that the Jest globals are not available:

VS Code intellisense underlining Jest in red

I have the Jest typedefs installed in my dev dependencies.

"devDependencies": {
    // ...truncated
    "@types/jest": "^20",
    "jest": "^20.0.4",
    "ts-jest": "^20.0.7",
    "ts-node": "^5.0.0",
    "typescript": "~2.4.0"
}
skyboyer
  • 22,209
  • 7
  • 57
  • 64
Steven Musumeche
  • 2,886
  • 5
  • 33
  • 55

3 Answers3

10

Correct answer here is that typescript requires type declarations for jest before jest global objects are visible for intellisense.

Add this triple-slash directive to beginning of your test file:

/// <reference types="jest" />
3

I've similarly struggled with this problem a number of times despite having @types/jest in my devDependencies too.

I created jest-without-globals as a very tiny wrapper to support importing Jest's features instead of relying on globals, thereby ensuring the variables exist.
It's written in TypeScript as well, ensuring that it's typed properly when imported and that you don't need to do anything other than an import to make the types function.

Per the Usage docs, it's straightforward to use:

import { describe, it, expect } from 'jest-without-globals'

describe('describe should create a section', () => {
  it('it should checkmark', () => {
    expect('').toBe('')
   })
})

All of the functions available in Jest's API, as well as jest and expect, can be imported from jest-without-globals.

agilgur5
  • 667
  • 12
  • 30
0

I upgraded my version of Typescript to 2.8 and this problem went away. I'm going to assume it was some sort of cache issue.

Steven Musumeche
  • 2,886
  • 5
  • 33
  • 55
  • 1
    I have seen that using the "Restart TS Server" command from Cmd+Shift+P can help sometimes. Ensuring my tsconfig file was being loaded and had the correct settings was also important, as was this post: https://stackoverflow.com/questions/57874114/intellisense-for-jest-not-working-in-vs-code – killthrush Nov 23 '21 at 21:25