You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code无法识别Jest全局变量,IntelliSense标红求助

Fixing VS Code IntelliSense Issues with Jest Global Variables in TypeScript Projects

Hey there, let’s tackle that annoying underline problem with Jest globals like test or expect in VS Code. I’ve run into this exact headache before, so here are the most reliable fixes to get IntelliSense working smoothly again:

1. Explicitly Add Jest Types to Your tsconfig.json

First up, make sure your TypeScript config is explicitly pulling in Jest’s type definitions. Open your tsconfig.json and add "jest" to the types array under compilerOptions:

{
  "compilerOptions": {
    // ... your existing settings
    "types": ["jest"]
  }
}

If you use a separate config file for tests (like tsconfig.test.json), apply the same change there too—this ensures your test files pick up the Jest types correctly.

2. Ensure VS Code Uses Your Project’s TypeScript Version

VS Code sometimes defaults to its built-in TypeScript version, which might clash with your project’s dependencies. To fix this:

  • Open any TypeScript file in VS Code
  • Look at the bottom-right corner of the window—you’ll see the current TypeScript version number
  • Click on it, then select "Use Workspace Version" from the dropdown menu

This forces VS Code to use the exact TypeScript version installed in your project’s node_modules.

3. Restart the TypeScript Language Service

Sometimes the language service just gets stuck. You can restart it without closing VS Code:

  • Open the command palette (press Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on Mac)
  • Search for and run the command: TypeScript: Restart TS Server

Give it a few seconds, then check if those underlines have disappeared.

4. Double-Check Your ts-jest Configuration

Since you’re using ts-jest, make sure your jest.config.js (or .ts) has the correct preset set:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  // ... other Jest settings
};

This tightens the integration between TypeScript and Jest, which can indirectly resolve IntelliSense quirks.

5. Clean Reinstall Dependencies (Last Resort)

If none of the above works, try wiping your node_modules and reinstalling everything—corrupted dependencies can cause weird type issues:

rm -rf node_modules package-lock.json yarn.lock
npm install

One quick bonus tip: make sure your test files follow standard naming conventions (like *.test.ts or *.spec.ts)—VS Code uses this pattern to automatically recognize test files and apply the right type definitions.

内容的提问来源于stack exchange,提问作者Steven Musumeche

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:30:27