Bun 的測試執行器現在支援內建的程式碼覆蓋率報告。這讓你可以輕鬆地查看有多少程式碼庫被測試涵蓋,並找出目前測試不足的地方。
啟用覆蓋率
bun:test
支援查看哪些程式碼行被測試涵蓋。若要使用此功能,請將 --coverage
傳遞給 CLI。它會將覆蓋率報告列印到主控台
$ bun test --coverage
-------------|---------|---------|-------------------
File | % Funcs | % Lines | Uncovered Line #s
-------------|---------|---------|-------------------
All files | 38.89 | 42.11 |
index-0.ts | 33.33 | 36.84 | 10-15,19-24
index-1.ts | 33.33 | 36.84 | 10-15,19-24
index-10.ts | 33.33 | 36.84 | 10-15,19-24
index-2.ts | 33.33 | 36.84 | 10-15,19-24
index-3.ts | 33.33 | 36.84 | 10-15,19-24
index-4.ts | 33.33 | 36.84 | 10-15,19-24
index-5.ts | 33.33 | 36.84 | 10-15,19-24
index-6.ts | 33.33 | 36.84 | 10-15,19-24
index-7.ts | 33.33 | 36.84 | 10-15,19-24
index-8.ts | 33.33 | 36.84 | 10-15,19-24
index-9.ts | 33.33 | 36.84 | 10-15,19-24
index.ts | 100.00 | 100.00 |
-------------|---------|---------|-------------------
若要預設總是啟用覆蓋率報告,請將下列程式碼行新增到 bunfig.toml
[test]
# always enable coverage
coverage = true
預設情況下,覆蓋率報告將包含測試檔案,並排除原始碼對應表。這通常是你想要的,但可以在 bunfig.toml
中另行設定。
[test]
coverageSkipTestFiles = true # default false
覆蓋率閾值
可以在 bunfig.toml
中指定覆蓋率閾值。如果你的測試套件未達到或超過此閾值,bun test
將會以非零退出碼退出,以表示失敗。
[test]
# to require 90% line-level and function-level coverage
coverageThreshold = 0.9
# to set different thresholds for lines and functions
coverageThreshold = { lines = 0.9, functions = 0.9 }
原始碼對應表
在內部,Bun 預設會轉譯所有檔案,因此 Bun 會自動產生一個內部的原始碼對應表,將你原始程式碼的行對應到 Bun 的內部表示。如果你因為任何原因想要停用此功能,請將 test.coverageIgnoreSourcemaps
設為 true
;除了進階使用案例之外,這很少會是理想的選擇。
[test]
coverageIgnoreSourcemaps = true # default false