mcpField
B
bun-test-coverage

bun-test-coverage

github.com/secondskyテスト・品質240409 回インストール5 週間前に更新
リポジトリ

bun-test-coverage について

Bunのテストランナーを使う開発者向けのコードカバレッジのスキルです。組み込みレポート、しきい値、レポーター形式、除外設定、CI構成を説明します。

主な機能

  1. 01コマンドラインでカバレッジを有効化
  2. 02カバレッジのしきい値を設定
  3. 03text、lcov、JSONレポートを生成
  4. 04除外対象と出力先を設定
  5. 05Bun.coverageからデータを取得

活用シーン

  1. 01CIでlcovレポートを生成
  2. 02カバレッジがしきい値未満ならテストを失敗
  3. 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コンソールのテーブル(デフォルト)
lcovCI ツール向けの coverage/lcov.info
jsoncoverage/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 を追加します

ベストプラクティス

  1. 現実的なしきい値を設定する - 60% から始めて、段階的に引き上げます
  2. 生成されたファイルを除外する - モックファイル、型定義
  3. 重要な処理経路に注力する - 定型コードよりビジネスロジックを優先します
  4. CI で実行する - カバレッジの低下を防ぎます

リファレンスを読み込むタイミング

次の場合は references/reporters.md を読み込みます。

  • カスタムレポーターの設定
  • CI/CD 統合の詳細
  • Codecov/Coveralls のセットアップ

コメント