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
<test output>
$ echo $?
1 # これは前のコマンドの終了コードです

行レベルと関数レベルのカバレッジに対して異なるしきい値を設定できます。

toml
[test]
# 行と関数に対して異なるしきい値を設定
coverageThreshold = { lines = 0.5, functions = 0.7 }

Bun でのコードカバレッジレポートの完全なドキュメントについては、ドキュメント > テストランナー > カバレッジ を参照してください。

Bun by www.bunjs.com.cn 編集