Husky Test Coverage
Set up or verify Husky git hooks to ensure tests run and coverage thresholds are enforced on every commit.
When to Use
- Setting up test coverage enforcement for the first time
- Verifying existing Husky/test setup is correctly configured
- Configuring pre-commit hooks for test coverage
- Adapting coverage setup to different test runners
Project Context Discovery
-
Check package.json:
- Review existing test scripts
- Detect test runner from dependencies (jest, vitest, mocha)
- Check for existing Husky installation
- Review existing coverage configuration
-
Identify Test Runner:
- Jest: Check for
jestin dependencies, look forjest.config.jsorjest.config.json - Vitest: Check for
vitestin dependencies, look forvitest.config.tsorvitest.config.js - Mocha: Check for
mochain dependencies, check for coverage tool (nyc, c8)
- Jest: Check for
-
Check Coverage Configuration:
- Jest: Look for
coverageThresholdin jest.config.* - Vitest: Look for
coverage.thresholdsin vitest.config.* - Mocha: Look for
.nycrc.jsonor coverage config in package.json
- Jest: Look for
-
Verify Existing Husky Setup:
- Check if
.husky/directory exists - Review existing pre-commit hook
- Check if Husky is in package.json dependencies
- Check if
-
Detect Test Files:
- Scan for
*.test.*or*.spec.*files - Verify tests exist before enforcing coverage
- Scan for
Quick Start
python3 ${CLAUDE_SKILL_DIR}/scripts/setup-husky-coverage.py --root /path/to/project
python3 ${CLAUDE_SKILL_DIR}/scripts/setup-husky-coverage.py --root /path/to/project --dry-run
See references/full-guide.md (§ Quick Start Examples) for threshold, warn-only, and skip-if-no-tests invocations.
What Gets Configured
Husky Setup
- Installs Husky if not already present
- Initializes Husky (
bunx husky install) - Creates
.husky/pre-commithook that runs tests with coverage - Adds
preparescript to package.json (if missing)
Test Runner Detection
The skill automatically detects:
- Jest: Uses
jest --coverage --watchAll=falsein pre-commit hook - Vitest: Uses
vitest --coverage --runin pre-commit hook - Mocha: Uses
nycorc8with mocha test command
Coverage Configuration
Jest:
- Creates or updates
jest.config.jsonwithcoverageThreshold - Default thresholds: 80% lines, 75% branches, 80% functions, 80% statements
Vitest:
- Creates or updates
vitest.config.ts/jswith coverage thresholds - Configures v8 coverage provider
- Sets same default thresholds as Jest
Mocha + nyc:
- Creates or updates
.nycrc.jsonwith coverage thresholds - Configures text, html, and lcov reporters
Pre-commit Hook
The created hook:
- Runs tests with coverage before every commit
- Fails the commit if coverage is below threshold (configurable)
- Can skip if no test files are found (optional)
Configuration Options
Command Line Arguments
--root <path>: Project root directory (required)--threshold <number>: Coverage threshold percentage (default: 80)--fail-on-below: Fail commit if coverage below threshold (default: true)--no-fail-on-below: Allow commit even if coverage below threshold--skip-if-no-tests: Skip hook if no test files found--dry-run: Show what would be done without making changes
Configuration File
Create .husky-test-coverage.json in project root. See references/full-guide.md (§ .husky-test-coverage.json Example) for the full schema.
Package.json Configuration
Alternatively, add to package.json:
{
"huskyTestCoverage": {
"threshold": 80,
"failOnBelow": true
}
}
Tech Stack Adaptation
Jest Projects
Detection:
- Checks for
jestin dependencies - Looks for
jest.config.jsorjest.config.json
Configuration:
- Updates or creates
jest.config.jsonwith coverage thresholds - Pre-commit hook:
bun run test -- --coverage --watchAll=false
See references/full-guide.md (§ Example jest.config.json) for a full config example.
Vitest Projects
Detection:
- Checks for
vitestin dependencies - Looks for
vitest.config.tsorvitest.config.js
Configuration:
- Updates or creates Vitest config with coverage thresholds
- Pre-commit hook:
bun run test -- --coverage --run
See references/full-guide.md (§ Example vitest.config.ts) for a full config example.
Mocha Projects
Detection:
- Checks for
mochain dependencies - Checks for coverage tool (
nycorc8)
Configuration:
- Creates or updates
.nycrc.jsonfor nyc - Pre-commit hook:
nyc --reporter=text --reporter=html bun run test
See references/full-guide.md (§ Example .nycrc.json) for a full config example.
Package Manager Support
The skill automatically detects and uses:
- bun:
bun run test(preferred) - yarn:
yarn test - pnpm:
pnpm run test
Workflow
- Scan package.json for test runner, dependencies, existing Husky config, and coverage config files. Verify test files exist.
- Identify Jest, Vitest, or Mocha; detect coverage tool (built-in or nyc/c8); determine package manager.
- Install Husky if missing; initialize hooks; add
preparescript if needed. - Create or update coverage configuration; set thresholds (default 80%); configure reporters.
- Generate pre-commit hook script; set enforcement behavior (block or warn).
- Verify setup; test hook with a commit; adjust thresholds if needed.
Integration with Other Skills
| Skill | How It Works Together | |-------|----------------------| | fullstack-workspace-init | Auto-invoked after scaffolding; sets Vitest + 80% threshold + CI/CD. Run this skill separately only when adding to an existing project. | | linter-formatter-init | Both configure Husky; this skill covers test coverage, linter-formatter-init covers linting/formatting | | testing-expert | Uses testing patterns and coverage targets from testing-expert skill |
Manual Integration (existing projects)
python3 ${CLAUDE_SKILL_DIR}/scripts/setup-husky-coverage.py \
--root /path/to/project \
--threshold 80
Troubleshooting
Pre-commit hook not running
# Reinstall Husky
bunx husky install
chmod +x .husky/pre-commit
Coverage not being checked
- Verify test command includes coverage flag
- Check coverage configuration file exists and is correct
- Ensure coverage tool is installed (nyc/c8 for Mocha)
Hook fails even when tests pass
- Check coverage thresholds are achievable
- Review coverage report to see what's below threshold
- Consider adjusting thresholds or improving test coverage
Tests run but coverage not enforced
- Verify coverage configuration file has correct thresholds
- Check test runner supports coverage (Jest/Vitest have built-in, Mocha needs nyc/c8)
- Review pre-commit hook script for correct command
Multiple test runners detected
The skill uses the first detected runner in priority order: Vitest > Jest > Mocha
Resources
- Husky Documentation: https://typicode.github.io/husky/
- Jest Coverage: https://jestjs.io/docs/configuration#coveragethreshold-object
- Vitest Coverage: https://vitest.dev/config/#coverage
- nyc (Istanbul): https://github.com/istanbuljs/nyc