如何编写VSCode launch.json配置调试Node 18测试运行器测试?
解决Node 18测试运行器在VSCode中的调试配置问题
问题分析
你遇到的路径错误核心原因是:命令行中的tests/**/*.ts通配符由shell负责解析展开为实际文件列表,但VSCode默认的launch配置会直接把通配符作为单个参数传给Node.js,Node无法识别该通配符,因此提示找不到文件。此外未明确指定工作目录也可能导致路径解析异常。
正确的launch.json配置
修改配置,启用shell解析通配符并明确工作目录:
{ "version": "0.2.0", "configurations": [ { "name": "Debug Node 18 Tests", "type": "node", "request": "launch", "command": "node", "args": [ "--loader", "tsx", "--test", "--test-reporter=spec", "tests/**/*.ts" ], "shell": true, "console": "integratedTerminal", "sourceMaps": true, "internalConsoleOptions": "neverOpen", "cwd": "${workspaceFolder}" } ] }
关键配置说明
"shell": true:让VSCode通过系统shell执行命令,确保通配符被正确解析为实际的测试文件路径。"cwd": "${workspaceFolder}":明确指定命令执行的工作目录为当前VSCode工作区根目录,避免路径解析错误。- 保留
"sourceMaps": true:确保调试时能映射到TypeScript源码,而非编译后的JS文件。
验证方法
保存配置后,在VSCode的调试面板选择Debug Node 18 Tests启动调试,此时shell会自动展开tests/**/*.ts为目录下的所有测试文件,Node 18测试运行器能正确加载并执行测试,同时支持断点调试。
内容的提问来源于stack exchange,提问作者birgersp
相关产品推荐
相关产品推荐

