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 中程式碼覆蓋率報告的完整文件,請參閱文件 > 測試執行器 > 覆蓋率。