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 = { lines = 0.5, functions = 0.7 }

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