Skip to content

Bun 的测试运行器通过 --coverage 标志支持内置的代码覆盖率报告。

sh
bun test --coverage
txt
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。这要求你的代码库有 90% 的覆盖率。

toml
[test]
# 要求 90% 的行级和函数级覆盖率
coverageThreshold = 0.9

如果你的测试套件未达到此阈值,bun test 将以非零退出码退出以表示失败。

sh
bun test --coverage
txt
<测试输出>
$ echo $?
1 # 这是上一条命令的退出码

可以为行级和函数级覆盖率设置不同的阈值。

toml
[test]
# 为行和函数设置不同的阈值
coverageThreshold = { lines = 0.5, functions = 0.7 }

请参阅 文档 > 测试运行器 > 覆盖率 获取 Bun 中代码覆盖率报告的完整文档。

Bun学习网由www.bunjs.com.cn整理维护