Back to Hooks

Test Coverage Guard

PostToolUse

Checks test coverage after edits and warns if coverage drops below the configured threshold

testingcoveragequalityguard

Hook Script

#!/bin/bash
# Test Coverage Guard Hook
# Warns if test coverage drops below threshold after file edits

FILE_PATH="$1"
COVERAGE_THRESHOLD=${COVERAGE_THRESHOLD:-80}

# Skip non-source files
if [[ "$FILE_PATH" == *.json ]] || [[ "$FILE_PATH" == *.md ]] || [[ "$FILE_PATH" == *.yml ]] || [[ "$FILE_PATH" == *.css ]]; then
  exit 0
fi

# Skip test files themselves
if [[ "$FILE_PATH" == *"test"* ]] || [[ "$FILE_PATH" == *"spec"* ]] || [[ "$FILE_PATH" == *"__tests__"* ]]; then
  exit 0
fi

# Node.js projects with Jest
if [ -f "package.json" ] && grep -q "jest" package.json 2>/dev/null; then
  COVERAGE_OUTPUT=$(npx jest --coverage --coverageReporters=text-summary --silent 2>/dev/null | grep "Statements")
  if [ -n "$COVERAGE_OUTPUT" ]; then
    COVERAGE=$(echo "$COVERAGE_OUTPUT" | grep -o '[0-9]*\.[0-9]*' | head -1 | cut -d. -f1)
    if [ -n "$COVERAGE" ] && [ "$COVERAGE" -lt "$COVERAGE_THRESHOLD" ]; then
      echo "⚠️  Test coverage is $COVERAGE% (threshold: $COVERAGE_THRESHOLD%)"
      echo "Consider adding tests for the changed code."
      exit 1
    fi
  fi
fi

# Python projects with pytest-cov
if [ -f "pyproject.toml" ] || [ -f "setup.py" ]; then
  if python -m pytest --co -q 2>/dev/null | grep -q "test"; then
    COVERAGE_OUTPUT=$(python -m pytest --cov --cov-report=term-missing -q 2>/dev/null | grep "TOTAL")
    if [ -n "$COVERAGE_OUTPUT" ]; then
      COVERAGE=$(echo "$COVERAGE_OUTPUT" | grep -o '[0-9]*%' | tr -d '%')
      if [ -n "$COVERAGE" ] && [ "$COVERAGE" -lt "$COVERAGE_THRESHOLD" ]; then
        echo "⚠️  Test coverage is $COVERAGE% (threshold: $COVERAGE_THRESHOLD%)"
        echo "Consider adding tests for the changed code."
        exit 1
      fi
    fi
  fi
fi

exit 0

Settings Configuration

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "command": "./hooks/test-coverage-guard.sh"
      }
    ]
  }
}

How to use

  1. Create a hooks directory in your project: mkdir hooks
  2. Save the hook script as hooks/test-coverage-guard.sh
  3. Make it executable: chmod +x hooks/test-coverage-guard.sh
  4. Add the configuration to your Claude Code settings
  5. Restart Claude Code to apply changes