断点不触发主因是vscode未attach到jest进程,须在launch.json中显式配置runtimeexecutable指向node_modules/.bin/jest并添加--runinband参数,且确保npx jest --version能正常执行。

断点不触发,90% 是因为 VSCode 根本没 attach 到 Jest 进程,而不是代码或断点本身有问题。必须用 launch.json 显式指向 jest 可执行文件,并强制单进程运行。
确认 jest 命令在终端能跑通
这是所有调试的前提。VSCode 不会帮你装 Jest,也不会 fallback 到全局安装。
- 在 VSCode 集成终端(
Ctrl + `)中执行:npx jest --version—— 必须输出类似29.7.0的版本号 - 如果报
command not found: jest,运行:npm install --save-dev jest @types/jest ts-jest(TypeScript 项目必装ts-jest) - 别用
npm install -g jest:全局路径不被插件识别,node_modules/.bin/jest才是默认查找目标 - 用 pnpm?改用
pnpx jest --version;用 yarn?用yarn jest --version
launch.json 必须显式配置 runtimeExecutable 和 --runInBand
VSCode 默认不识别 npm test 或 npx jest 这类命令行,它只 attach Node.js 进程。直接点“Debug Test”本质是终端执行,调试器根本没挂上。
-
program字段不能填test.js或空值,必须填实际入口:"${workspaceFolder}/node_modules/.bin/jest"(macOS/Linux)或"${workspaceFolder}/node_modules/jest/bin/jest.js"(Windows) -
args中必须包含:"--runInBand"—— Jest 默认多进程,VSCode 调试器只能 attach 主进程,不加这个参数断点基本静默失效 - 若调试当前文件,补上:
"--runTestsByPath","${file}" - 避免用
runtimeExecutable: "npm"+args: ["test"]:部分包管理器(如 pnpm)可能不兼容该模式
sourceMap 对齐失败导致断点悬空
断点变成空心圆、停在 __tests__/xxx.js 或空白行?不是断点打错了,是 VSCode 找不到源码映射。
- TypeScript 项目:确保
tsconfig.json同时开启:"sourceMap": true和"inlineSources": true - Jest 配置中禁用 inline source map:
"sourceMap": false(jest.config.js),否则 VSCode 调试器不支持 inline 格式 -
launch.json中加"outFiles": ["./dist/**/*.js"](对应你 Jest 输出目录),否则 VSCode 不知道去哪找生成后的 JS 和.map文件 - 测试文件在
__tests__目录下?在jest.config.js的resolveSourceMapLocations中显式放行:["${workspaceFolder}/src/**", "${workspaceFolder}/__tests__/**"]
测试文件没出现在测试面板?先查 testMatch
Orta.vscode-jest 和 VSCode 原生 Test Explorer 都靠 testMatch 扫描文件。默认规则只认 **/__tests__/** 和 **/*.test.*,你的命名习惯很可能不匹配。
- 测试文件叫
Button.unit.tsx?在jest.config.js中加:"**/*.unit.{ts,tsx}" - 测试全放在
src/tests/下?改成:"<rootdir>/src/tests/**/*.{test,spec}.{js,ts,tsx}"</rootdir> - 别同时写
testMatch和testRegex:testMatch优先级更高,后者会被忽略 - 验证是否生效:终端运行
npx jest --listTests,看是否列出你的测试文件
最常被跳过的环节是:没确认 npx jest --version 是否真能执行,以及没在 launch.json 里显式写 --runInBand。这两步漏掉,后面所有断点配置都白搭。











