Bun 的测试运行器支持内置的代码覆盖率报告。这使得查看代码库中有多少被测试覆盖并找出当前未得到充分测试的区域变得容易。
将 --coverage 标志传递给 bun test 以启用此功能。这将在测试运行后打印覆盖率报告。
覆盖率报告列出测试运行期间执行的源文件、执行的函数和行数的百分比,以及运行期间未执行的行范围。
sh
bun test --coveragetxt
test.test.ts:
✓ math > add [0.71ms]
✓ math > multiply [0.03ms]
✓ random [0.13ms]
-------------|---------|---------|-------------------
File | % Funcs | % Lines | Uncovered Line #s
-------------|---------|---------|-------------------
All files | 66.67 | 77.78 |
math.ts | 50.00 | 66.67 |
random.ts | 50.00 | 66.67 |
-------------|---------|---------|-------------------
3 pass
0 fail
3 expect() calls要默认始终启用覆盖率报告,请将以下行添加到你的 bunfig.toml:
toml
[test]
coverage = true # 始终启用覆盖率请参阅 文档 > 测试运行器 > 覆盖率 获取 Bun 中代码覆盖率报告的完整文档。