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整理維護