VS Code启用Caught Exceptions时,Jest调试为何停在Yarn的cli.js?
解决VS Code调试Jest时跳过Yarn/Node核心文件的问题
问题描述
在VS Code中调试Jest测试,开启「Caught Exceptions」断点后,调试器总是停在和业务无关的/.node/corepack/yarn/1.22.15/lib/cli.js文件里。已经在launch.json的skipFiles中添加了多个路径,但完全没效果,求问怎么忽略这些Yarn/Node核心文件?
原launch.json配置:
{ "version": "0.2.0", "configurations": [ { "type": "node", "name": "vscode-jest-tests.v2", "request": "launch", "runtimeExecutable": "yarn", "runtimeVersion": "16.13.2", "program": "jest", "args": [ "--runInBand", "--watchAll=false", "--testNamePattern", "${jest.testNamePattern}", "--runTestsByPath", "${jest.testFile}" ], "cwd": "${workspaceFolder}/next/", "console": "integratedTerminal", "internalConsoleOptions": "neverOpen", "windows": { "program": "jest" }, "skipFiles": [ "<node_internals>/**/*.js", "~/.node/**/*.js", "**/.yarn/**", "**/node_modules/**", "**/yarn/**" ] } ] }
解决方案
方案一:精准匹配Corepack Yarn路径(最小改动)
原配置的skipFiles没精准命中Corepack管理的Yarn文件,同时存在JSON格式错误(最后一个元素多余逗号),修改如下:
- 移除
skipFiles数组最后一个元素的逗号(JSON不允许尾逗号) - 添加更精准的Corepack Yarn路径规则
修改后的skipFiles部分:
"skipFiles": [ "<node_internals>/**", "~/.node/corepack/**/*.js", "**/.yarn/**", "**/node_modules/**", "**/yarn/**" ]
方案二:直接用Node启动Jest(更稳定)
通过Yarn启动会把Yarn的cli文件加入调用栈,换成直接用Node运行Jest可从根源避免这个问题:
修改launch.json的runtimeExecutable和program字段:
{ "version": "0.2.0", "configurations": [ { "type": "node", "name": "vscode-jest-tests.v2", "request": "launch", "runtimeExecutable": "node", "runtimeVersion": "16.13.2", "program": "${workspaceFolder}/next/node_modules/jest/bin/jest.js", "args": [ "--runInBand", "--watchAll=false", "--testNamePattern", "${jest.testNamePattern}", "--runTestsByPath", "${jest.testFile}" ], "cwd": "${workspaceFolder}/next/", "console": "integratedTerminal", "internalConsoleOptions": "neverOpen", "skipFiles": [ "<node_internals>/**", "**/node_modules/**" ] } ] }
说明
- 方案一的关键是
~/.node/corepack/**/*.js精准匹配Corepack安装的Yarn文件,同时修复JSON格式错误确保配置生效 - 方案二跳过了Yarn的启动流程,直接让Node运行Jest,调用栈里不会出现Yarn的cli文件,调试体验更干净
内容的提问来源于stack exchange,提问作者André Ricardo
相关产品推荐
相关产品推荐

