bun-test-coverage について
Bunのテストランナーを使う開発者向けのコードカバレッジのスキルです。組み込みレポート、しきい値、レポーター形式、除外設定、CI構成を説明します。
主な機能
- 01コマンドラインでカバレッジを有効化
- 02カバレッジのしきい値を設定
- 03text、lcov、JSONレポートを生成
- 04除外対象と出力先を設定
- 05Bun.coverageからデータを取得
活用シーン
- 01CIでlcovレポートを生成
- 02カバレッジがしきい値未満ならテストを失敗
- 03未カバー行を確認してテストを調整
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ secondsky/ claude-skills --skill bun-test-coverageインストール後、Claude Code で/bun-test-coverageを入力して読み込まれたか確認します。
詳細
Bun のテストカバレッジ
Bun には、追加の依存関係なしで使えるコードカバレッジレポート機能が組み込まれています。
カバレッジの有効化
# Enable coverage
bun test --coverage
# With threshold (fail if below)
bun test --coverage --coverage-threshold 80
bunfig.toml での設定
[test]
coverage = true
coverageThreshold = 0.8 # 80% minimum
coverageDir = "./coverage"
# Patterns to ignore
coverageSkipTestFiles = true
カバレッジの出力
------------------|---------|---------|-------------------
File | % Funcs | % Lines | Uncovered Line #s
------------------|---------|---------|-------------------
All files | 85.71 | 89.23 |
src/index.ts | 100.00 | 100.00 |
src/utils.ts | 75.00 | 82.35 | 23-25, 41-43
src/api.ts | 80.00 | 85.00 | 67, 89-92
------------------|---------|---------|-------------------
カバレッジレポーター
# Default console output
bun test --coverage
# Generate lcov report
bun test --coverage --coverage-reporter=lcov
# Multiple reporters
bun test --coverage --coverage-reporter=text --coverage-reporter=lcov
利用可能なレポーター
| レポーター | 出力 |
|---|---|
text | コンソールのテーブル(デフォルト) |
lcov | CI ツール向けの coverage/lcov.info |
json | coverage/coverage.json |
カバレッジのしきい値
最低限必要なカバレッジを設定します。
# Fail if coverage < 80%
bun test --coverage --coverage-threshold 80
# Per-metric thresholds in bunfig.toml
[test]
coverage = true
coverageThreshold = {
lines = 80,
functions = 75,
branches = 70
}
ファイルの除外
[test]
coverage = true
# Skip test files from coverage
coverageSkipTestFiles = true
# Patterns to exclude
coverageIgnore = [
"**/*.test.ts",
"**/fixtures/**",
"**/mocks/**"
]
CI との統合
GitHub Actions
- name: Run tests with coverage
run: bun test --coverage --coverage-reporter=lcov
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
files: ./coverage/lcov.info
出力ディレクトリ
# Custom output directory
bun test --coverage --coverage-dir=./reports/coverage
プログラムからのカバレッジ利用
import { test, expect } from "bun:test";
// Get coverage data programmatically
const coverage = Bun.coverage;
// Access after tests complete
process.on("exit", () => {
console.log(coverage.getCoverageData());
});
よくあるエラー
| エラー | 原因 | 対処法 |
|---|---|---|
Coverage threshold not met | カバレッジがしきい値を下回っています | テストカバレッジを増やします |
No coverage data | ファイルが実行されていません | テストにファイルが含まれているか確認します |
lcov not found | レポーターが不足しています | --coverage-reporter=lcov を追加します |
ベストプラクティス
- 現実的なしきい値を設定する - 60% から始めて、段階的に引き上げます
- 生成されたファイルを除外する - モックファイル、型定義
- 重要な処理経路に注力する - 定型コードよりビジネスロジックを優先します
- CI で実行する - カバレッジの低下を防ぎます
リファレンスを読み込むタイミング
次の場合は references/reporters.md を読み込みます。
- カスタムレポーターの設定
- CI/CD 統合の詳細
- Codecov/Coveralls のセットアップ
コメント
Markdown 対応