Bun

指南測試執行器

使用 Bun 測試執行器設定程式碼覆蓋率門檻

Bun 的測試執行器透過 --coverage 旗標支援內建程式碼覆蓋率回報。

bun test --coverage

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% 的程式碼庫必須由測試覆蓋。

[test]
# to require 90% line-level and function-level coverage
coverageThreshold = 0.9

如果你的測試套件未達到此門檻,bun test 將會傳回非零結束代碼以表示失敗。

bun test --coverage
<test output>
echo $?
1 # this is the exit code of the previous command

可以為程式碼行等級和函式等級覆蓋率設定不同的門檻。

[test]
# to set different thresholds for lines and functions
coverageThreshold = { line = 0.5, function = 0.7 }

請參閱 文件 > 測試執行器 > 覆蓋率,以取得 Bun 中程式碼覆蓋率回報的完整文件。