使用 Bun 测试运行器设置代码覆盖率阈值
Bun 的测试运行器内置代码覆盖率报告功能。使用 --coverage 标志启用该功能。
terminal
bun test --coverage
Text
test.test.ts:
✓ math > add [0.71ms]
✓ math > multiply [0.03ms]
✓ random [0.13ms]
-------------|---------|---------|-------------------
文件 | 函数覆盖率 | 代码行覆盖率 | 未覆盖的行号
-------------|---------|---------|-------------------
All files | 50.00 | 66.67 |
math.ts | 50.00 | 66.67 |
random.ts | 50.00 | 66.67 |
-------------|---------|---------|-------------------
3 通过
0 失败
3 次 expect() 调用
若要设置最低覆盖率阈值,请将以下内容添加到 bunfig.toml。0.9 的阈值要求覆盖率报告中的每个文件,其代码行和函数覆盖率均达到 90%。Bun 会针对每个文件检查阈值,而不是针对 All files 的平均值。
bunfig.toml
[test]
# 要求 90% 的代码行和函数覆盖率
coverageThreshold = 0.9
如果测试套件未达到此阈值,bun test 将以非零退出码退出,以表示失败。
terminal
bun test --coverage
Text
<测试输出>
$ echo $?
1 # 这是前一个命令的退出码
你可以为代码行覆盖率和函数覆盖率设置不同的阈值。
bunfig.toml
[test]
# 为代码行和函数设置不同的阈值
coverageThreshold = { lines = 0.5, functions = 0.7 }
请参阅代码覆盖率。