vscode 中需配置 tasks.json、命名规范、安装 bun test explorer 插件及 launch.json 才能正常运行、发现、显示和调试 bun 测试。

VSCode 里直接运行 Bun 测试失败:缺 task 配置
Bun 自带 bun test,但 VSCode 默认不认识它——没配置 tasks.json,右键“Run Test”或点击测试旁的 ▶️ 按钮会静默失败或报错 command 'testing.run' not found。
必须手动告诉 VSCode:用 bun test 当测试执行器,且要支持 --watch、--filter 等参数。
- 在项目根目录建
.vscode/tasks.json - 写入标准
shell类型 task,label设为test(VSCode 测试面板默认找这个 label) -
command必须写全路径或确保bun在 PATH 中;推荐用npx bun test避免环境差异 - 加
"group": "test",否则 VSCode 不识别为测试任务
{
"version": "2.0.0",
"tasks": [
{
"label": "test",
"type": "shell",
"command": "bun test",
"args": ["${fileBasename}"],
"group": "test",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": true
},
"problemMatcher": []
}
]
}
Bun 测试不自动发现:文件名/路径不符合约定
Bun 默认只跑 *.{test,spec}.{js,ts,jsx,tsx} 文件,且要求在 test 目录或顶层——如果文件叫 utils_test.ts 或放在 src/__tests__/,bun test 会直接跳过。
- 改名:用
utils.test.ts而非utils_test.ts - 别嵌太深:把测试文件放
test/下,或和源码同级(如src/utils.ts对应src/utils.test.ts) - 强制指定:调试时可在终端手动运行
bun test src/utils.test.ts验证单个文件是否可执行 - 注意大小写:
.Test.ts不匹配,必须是小写.test.ts
VSCode 测试侧边栏显示“no tests found”:缺少测试适配器
VSCode 的测试 UI(左侧面板 Test 图标)依赖测试适配器协议(Test Adapter Protocol),Bun 原生不提供。不装插件,就只能靠终端或 tasks.json 手动跑,侧边栏永远空。
- 装官方插件:
Bun Test Explorer(作者:jnoortheen)——目前唯一稳定支持 Bun 的测试浏览器 - 装完重启 VSCode,它会自动扫描
bun test --list输出并渲染测试树 - 注意:该插件依赖项目有
bun.lockb且bun可执行;若提示 “bun not found”,检查PATH或在插件设置里填绝对路径(如/opt/homebrew/bin/bun) - 不兼容旧版 Bun:v1.1.0+ 才支持
--json输出,插件需要它解析结果;低于此版本会卡住
断点调试 Bun 测试进不去:没启用 source map 或 launch 配置错
VSCode 调试器默认不理解 bun test 的运行时上下文,直接点调试按钮会启动失败或断点失效。
- 必须用
.vscode/launch.json配置runtimeExecutable指向bun -
args设为["test", "--inspect-brk", "${file}"],让 Bun 启动调试服务 -
sourceMaps设为true,且确保 TS 编译输出含.map文件(tsconfig.json里"sourceMap": true) - 别用 Node.js 预设:选 “Bun” 环境(如果插件支持)或手动设
type: "pwa-node"+runtimeExecutable
最简能用的 launch.json 片段:
{
"configurations": [
{
"name": "Debug Bun Test",
"type": "pwa-node",
"request": "launch",
"runtimeExecutable": "bun",
"args": ["test", "--inspect-brk", "${file}"],
"console": "integratedTerminal",
"sourceMaps": true,
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
}
]
}
测试文件里打 debugger,再按 F5,才能真正停住——光写 bun test 是不会触发 VSCode 调试通道的。











