husky-test-coverage

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Husky Test Coverage

Husky 测试覆盖率

Set up or verify Husky git hooks to ensure tests run and coverage thresholds are enforced on every commit.
设置或验证Husky git钩子,确保每次提交时都会运行测试并强制执行覆盖率阈值。

Purpose

用途

This skill automates the setup of:
  • Husky git hooks for pre-commit testing
  • Test runner detection (Jest, Vitest, Mocha)
  • Coverage configuration with thresholds (default: 80%)
  • Pre-commit hook that runs tests with coverage
  • Configurable coverage enforcement (block or warn)
此技能可自动设置以下内容:
  • 用于预提交测试的Husky git钩子
  • 测试运行器检测(Jest、Vitest、Mocha)
  • 带阈值的覆盖率配置(默认值:80%)
  • 运行带覆盖率测试的预提交钩子
  • 可配置的覆盖率强制执行(阻止提交或仅警告)

When to Use

适用场景

This skill should be used when:
  • Setting up test coverage enforcement for the first time
  • Verifying existing Husky/test setup is correctly configured
  • Ensuring coverage thresholds are met before commits
  • Configuring pre-commit hooks for test coverage
  • Adapting coverage setup to different test runners
在以下场景中应使用此技能:
  • 首次设置测试覆盖率强制执行
  • 验证现有Husky/测试设置是否配置正确
  • 确保提交前达到覆盖率阈值
  • 为测试覆盖率配置预提交钩子
  • 针对不同测试运行器调整覆盖率设置

Project Context Discovery

项目上下文探测

Before setting up test coverage, discover the project's context:
  1. Check package.json:
    • Review existing test scripts
    • Detect test runner from dependencies (jest, vitest, mocha)
    • Check for existing Husky installation
    • Review existing coverage configuration
  2. Identify Test Runner:
    • Jest: Check for
      jest
      in dependencies, look for
      jest.config.js
      or
      jest.config.json
    • Vitest: Check for
      vitest
      in dependencies, look for
      vitest.config.ts
      or
      vitest.config.js
    • Mocha: Check for
      mocha
      in dependencies, check for coverage tool (nyc, c8)
  3. Check Coverage Configuration:
    • Jest: Look for
      coverageThreshold
      in jest.config.*
    • Vitest: Look for
      coverage.thresholds
      in vitest.config.*
    • Mocha: Look for
      .nycrc.json
      or coverage config in package.json
  4. Verify Existing Husky Setup:
    • Check if
      .husky/
      directory exists
    • Review existing pre-commit hook
    • Check if Husky is in package.json dependencies
  5. Detect Test Files:
    • Scan for
      *.test.*
      or
      *.spec.*
      files
    • Verify tests exist before enforcing coverage
在设置测试覆盖率之前,先探测项目的上下文:
  1. 检查package.json:
    • 查看现有测试脚本
    • 从依赖项中检测测试运行器(jest、vitest、mocha)
    • 检查是否已安装Husky
    • 查看现有覆盖率配置
  2. 识别测试运行器:
    • Jest:检查依赖项中是否有
      jest
      ,查找
      jest.config.js
      jest.config.json
    • Vitest:检查依赖项中是否有
      vitest
      ,查找
      vitest.config.ts
      vitest.config.js
    • Mocha:检查依赖项中是否有
      mocha
      ,检查覆盖率工具(nyc、c8)
  3. 检查覆盖率配置:
    • Jest:在jest.config.*中查找
      coverageThreshold
    • Vitest:在vitest.config.*中查找
      coverage.thresholds
    • Mocha:查找
      .nycrc.json
      或package.json中的覆盖率配置
  4. 验证现有Husky设置:
    • 检查是否存在
      .husky/
      目录
    • 查看现有预提交钩子
    • 检查package.json依赖项中是否有Husky
  5. 检测测试文件:
    • 扫描
      *.test.*
      *.spec.*
      文件
    • 在强制执行覆盖率之前验证测试是否存在

Quick Start

快速开始

bash
undefined
bash
undefined

Basic setup (80% coverage threshold, blocks commits below threshold)

Basic setup (80% coverage threshold, blocks commits below threshold)

python3 ~/.claude/skills/husky-test-coverage/scripts/setup-husky-coverage.py
--root /path/to/project
python3 ~/.claude/skills/husky-test-coverage/scripts/setup-husky-coverage.py
--root /path/to/project

Custom threshold (85%)

Custom threshold (85%)

python3 ~/.claude/skills/husky-test-coverage/scripts/setup-husky-coverage.py
--root /path/to/project
--threshold 85
python3 ~/.claude/skills/husky-test-coverage/scripts/setup-husky-coverage.py
--root /path/to/project
--threshold 85

Warn only (don't block commits)

Warn only (don't block commits)

python3 ~/.claude/skills/husky-test-coverage/scripts/setup-husky-coverage.py
--root /path/to/project
--no-fail-on-below
python3 ~/.claude/skills/husky-test-coverage/scripts/setup-husky-coverage.py
--root /path/to/project
--no-fail-on-below

Skip if no tests found

Skip if no tests found

python3 ~/.claude/skills/husky-test-coverage/scripts/setup-husky-coverage.py
--root /path/to/project
--skip-if-no-tests
python3 ~/.claude/skills/husky-test-coverage/scripts/setup-husky-coverage.py
--root /path/to/project
--skip-if-no-tests

Dry run to preview changes

Dry run to preview changes

python3 ~/.claude/skills/husky-test-coverage/scripts/setup-husky-coverage.py
--root /path/to/project
--dry-run
undefined
python3 ~/.claude/skills/husky-test-coverage/scripts/setup-husky-coverage.py
--root /path/to/project
--dry-run
undefined

What Gets Configured

配置内容

Husky Setup

Husky 设置

  • Installs Husky if not already present
  • Initializes Husky (
    npx husky install
    )
  • Creates
    .husky/pre-commit
    hook that runs tests with coverage
  • Adds
    prepare
    script to package.json (if missing)
  • 如果尚未安装则安装Husky
  • 初始化Husky(
    npx husky install
  • 创建
    .husky/pre-commit
    钩子,运行带覆盖率的测试
  • 向package.json添加
    prepare
    脚本(如果缺失)

Test Runner Detection

测试运行器检测

The skill automatically detects:
  • Jest: Uses
    jest --coverage --watchAll=false
    in pre-commit hook
  • Vitest: Uses
    vitest --coverage --run
    in pre-commit hook
  • Mocha: Uses
    nyc
    or
    c8
    with mocha test command
此技能会自动检测:
  • Jest:在预提交钩子中使用
    jest --coverage --watchAll=false
  • Vitest:在预提交钩子中使用
    vitest --coverage --run
  • Mocha:将
    nyc
    c8
    与mocha测试命令配合使用

Coverage Configuration

覆盖率配置

Jest:
  • Creates or updates
    jest.config.json
    with
    coverageThreshold
  • Default thresholds: 80% lines, 75% branches, 80% functions, 80% statements
Vitest:
  • Creates or updates
    vitest.config.ts/js
    with coverage thresholds
  • Configures v8 coverage provider
  • Sets same default thresholds as Jest
Mocha + nyc:
  • Creates or updates
    .nycrc.json
    with coverage thresholds
  • Configures text, html, and lcov reporters
Jest:
  • 创建或更新
    jest.config.json
    ,添加
    coverageThreshold
  • 默认阈值:80%行覆盖率、75%分支覆盖率、80%函数覆盖率、80%语句覆盖率
Vitest:
  • 创建或更新
    vitest.config.ts/js
    ,添加覆盖率阈值
  • 配置v8覆盖率提供器
  • 设置与Jest相同的默认阈值
Mocha + nyc:
  • 创建或更新
    .nycrc.json
    ,添加覆盖率阈值
  • 配置text、html和lcov报告器

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
  • --root <path>
    :项目根目录(必填)
  • --threshold <number>
    :覆盖率阈值百分比(默认:80)
  • --fail-on-below
    :如果覆盖率低于阈值则阻止提交(默认:true)
  • --no-fail-on-below
    :即使覆盖率低于阈值也允许提交
  • --skip-if-no-tests
    :如果无测试文件则跳过钩子
  • --dry-run
    :显示将执行的操作但不进行实际更改

Configuration File

配置文件

Create
.husky-test-coverage.json
in project root:
json
{
  "coverageThreshold": {
    "lines": 80,
    "branches": 75,
    "functions": 80,
    "statements": 80
  },
  "failOnCoverageBelowThreshold": true,
  "skipIfNoTests": false
}
在项目根目录创建
.husky-test-coverage.json
json
{
  "coverageThreshold": {
    "lines": 80,
    "branches": 75,
    "functions": 80,
    "statements": 80
  },
  "failOnCoverageBelowThreshold": true,
  "skipIfNoTests": false
}

Package.json Configuration

Package.json 配置

Alternatively, add to
package.json
:
json
{
  "huskyTestCoverage": {
    "threshold": 80,
    "failOnBelow": true
  }
}
或者,添加到
package.json
json
{
  "huskyTestCoverage": {
    "threshold": 80,
    "failOnBelow": true
  }
}

Tech Stack Adaptation

技术栈适配

Jest Projects

Jest 项目

Detection:
  • Checks for
    jest
    in dependencies
  • Looks for
    jest.config.js
    or
    jest.config.json
Configuration:
  • Updates or creates
    jest.config.json
    with coverage thresholds
  • Pre-commit hook:
    npm test -- --coverage --watchAll=false
Example jest.config.json:
json
{
  "coverageThreshold": {
    "global": {
      "lines": 80,
      "branches": 75,
      "functions": 80,
      "statements": 80
    }
  }
}
检测:
  • 检查依赖项中是否有
    jest
  • 查找
    jest.config.js
    jest.config.json
配置:
  • 更新或创建
    jest.config.json
    ,添加覆盖率阈值
  • 预提交钩子:
    npm test -- --coverage --watchAll=false
示例 jest.config.json:
json
{
  "coverageThreshold": {
    "global": {
      "lines": 80,
      "branches": 75,
      "functions": 80,
      "statements": 80
    }
  }
}

Vitest Projects

Vitest 项目

Detection:
  • Checks for
    vitest
    in dependencies
  • Looks for
    vitest.config.ts
    or
    vitest.config.js
Configuration:
  • Updates or creates Vitest config with coverage thresholds
  • Pre-commit hook:
    npm test -- --coverage --run
Example vitest.config.ts:
typescript
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    coverage: {
      provider: 'v8',
      reporter: ['text', 'json', 'html'],
      thresholds: {
        lines: 80,
        branches: 75,
        functions: 80,
        statements: 80
      }
    }
  }
})
检测:
  • 检查依赖项中是否有
    vitest
  • 查找
    vitest.config.ts
    vitest.config.js
配置:
  • 更新或创建Vitest配置,添加覆盖率阈值
  • 预提交钩子:
    npm test -- --coverage --run
示例 vitest.config.ts:
typescript
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    coverage: {
      provider: 'v8',
      reporter: ['text', 'json', 'html'],
      thresholds: {
        lines: 80,
        branches: 75,
        functions: 80,
        statements: 80
      }
    }
  }
})

Mocha Projects

Mocha 项目

Detection:
  • Checks for
    mocha
    in dependencies
  • Checks for coverage tool (
    nyc
    or
    c8
    )
Configuration:
  • Creates or updates
    .nycrc.json
    for nyc
  • Pre-commit hook:
    nyc --reporter=text --reporter=html npm test
Example .nycrc.json:
json
{
  "check-coverage": true,
  "lines": 80,
  "branches": 75,
  "functions": 80,
  "statements": 80,
  "reporter": ["text", "text-summary", "html", "lcov"]
}
检测:
  • 检查依赖项中是否有
    mocha
  • 检查覆盖率工具(
    nyc
    c8
配置:
  • 为nyc创建或更新
    .nycrc.json
  • 预提交钩子:
    nyc --reporter=text --reporter=html npm test
示例 .nycrc.json:
json
{
  "check-coverage": true,
  "lines": 80,
  "branches": 75,
  "functions": 80,
  "statements": 80,
  "reporter": ["text", "text-summary", "html", "lcov"]
}

Package Manager Support

包管理器支持

The skill automatically detects and uses:
  • npm:
    npm run test
  • yarn:
    yarn test
  • pnpm:
    pnpm run test
  • bun:
    bun run test
此技能会自动检测并使用:
  • npm
    npm run test
  • yarn
    yarn test
  • pnpm
    pnpm run test
  • bun
    bun run test

Workflow

工作流程

When using this skill:
  1. Discover Project Context:
    • Scan package.json for test runner and dependencies
    • Check existing Husky configuration
    • Review existing coverage config files
    • Verify test files exist
  2. Detect Test Runner:
    • Identify Jest, Vitest, or Mocha
    • Detect coverage tool (built-in or nyc/c8)
    • Determine package manager
  3. Setup or Verify Husky:
    • Install Husky if missing
    • Initialize Husky hooks
    • Add prepare script if needed
  4. Configure Coverage:
    • Create or update coverage configuration
    • Set coverage thresholds (default 80%)
    • Configure appropriate reporters
  5. Create Pre-commit Hook:
    • Generate hook script with test command
    • Configure to run tests with coverage
    • Set enforcement behavior (block or warn)
  6. Verify Setup:
    • Review generated configuration
    • Test hook with a commit
    • Adjust thresholds if needed
使用此技能时:
  1. 探测项目上下文:
    • 扫描package.json以获取测试运行器和依赖项
    • 检查现有Husky配置
    • 查看现有覆盖率配置文件
    • 验证测试文件是否存在
  2. 检测测试运行器:
    • 识别Jest、Vitest或Mocha
    • 检测覆盖率工具(内置或nyc/c8)
    • 确定包管理器
  3. 设置或验证Husky:
    • 如果缺失则安装Husky
    • 初始化Husky钩子
    • 必要时添加prepare脚本
  4. 配置覆盖率:
    • 创建或更新覆盖率配置
    • 设置覆盖率阈值(默认80%)
    • 配置合适的报告器
  5. 创建预提交钩子:
    • 生成带测试命令的钩子脚本
    • 配置为运行带覆盖率的测试
    • 设置强制执行行为(阻止或警告)
  6. 验证设置:
    • 查看生成的配置
    • 通过提交测试钩子
    • 必要时调整阈值

Integration with Other Skills

与其他技能的集成

This skill works alongside:
SkillHow It Works Together
fullstack-workspace-initAutomatically invoked after scaffolding to set up 80% coverage threshold
linter-formatter-initBoth configure Husky; this skill focuses on test coverage, linter-formatter-init focuses on linting/formatting
testing-expertUses testing patterns and coverage targets from testing-expert skill
此技能可与以下技能配合使用:
技能配合方式
fullstack-workspace-init在脚手架搭建后自动调用,设置80%覆盖率阈值
linter-formatter-init两者均配置Husky;此技能专注于测试覆盖率,linter-formatter-init专注于代码检查/格式化
testing-expert使用testing-expert技能中的测试模式和覆盖率目标

Automatic Setup with fullstack-workspace-init

与fullstack-workspace-init的自动设置

When using
fullstack-workspace-init
to scaffold a new project, this skill is automatically applied with:
  • Vitest as the test runner
  • 80% coverage threshold
  • Pre-commit hooks enabled
  • GitHub Actions CI/CD integration
You don't need to run this skill separately if you used
fullstack-workspace-init
.
使用
fullstack-workspace-init
搭建新项目时,会自动应用此技能并配置:
  • Vitest作为测试运行器
  • 80%覆盖率阈值
  • 启用预提交钩子
  • GitHub Actions CI/CD集成
如果使用了
fullstack-workspace-init
,则无需单独运行此技能。

Manual Integration

手动集成

If adding to an existing project:
bash
python3 ~/.claude/skills/husky-test-coverage/scripts/setup-husky-coverage.py \
  --root /path/to/project \
  --threshold 80
如果添加到现有项目:
bash
python3 ~/.claude/skills/husky-test-coverage/scripts/setup-husky-coverage.py \
  --root /path/to/project \
  --threshold 80

Troubleshooting

故障排除

Pre-commit hook not running

预提交钩子未运行

bash
undefined
bash
undefined

Reinstall Husky

Reinstall Husky

npx husky install chmod +x .husky/pre-commit
undefined
npx husky install chmod +x .husky/pre-commit
undefined

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)
  • 验证测试命令包含覆盖率标志
  • 检查覆盖率配置文件是否存在且正确
  • 确保已安装覆盖率工具(Mocha需要nyc/c8)

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
  • 验证覆盖率配置文件具有正确的阈值
  • 检查测试运行器是否支持覆盖率(Jest/Vitest内置支持,Mocha需要nyc/c8)
  • 查看预提交钩子脚本中的命令是否正确

Multiple test runners detected

检测到多个测试运行器

The skill uses the first detected runner in priority order: Vitest > Jest > Mocha
此技能会按优先级顺序使用第一个检测到的运行器:Vitest > Jest > Mocha

Best Practices

最佳实践

  1. Start with reasonable thresholds: Begin with 80% and adjust based on project needs
  2. Monitor coverage trends: Use coverage reports to identify gaps
  3. Incremental improvement: Gradually increase thresholds as coverage improves
  4. Consider context: Some files (utilities, configs) may not need high coverage
  5. Use with CI/CD: Pre-commit hooks catch issues early, but CI/CD provides final gate
  6. Team alignment: Ensure team understands coverage requirements and goals
  1. 从合理的阈值开始: 从80%开始,根据项目需求调整
  2. 监控覆盖率趋势: 使用覆盖率报告识别差距
  3. 逐步改进: 随着覆盖率提高逐渐增加阈值
  4. 考虑上下文: 某些文件(工具类、配置文件)可能不需要高覆盖率
  5. 与CI/CD配合使用: 预提交钩子提前发现问题,但CI/CD提供最终保障
  6. 团队对齐: 确保团队了解覆盖率要求和目标

Resources

资源


When this skill is active, Claude will:
  1. Discover project test setup and configuration
  2. Detect test runner and coverage tool
  3. Set up or verify Husky installation
  4. Configure coverage thresholds appropriately
  5. Create pre-commit hook that enforces coverage
  6. Provide troubleshooting guidance when needed

当此技能激活时,Claude会:
  1. 探测项目测试设置和配置
  2. 检测测试运行器和覆盖率工具
  3. 设置或验证Husky安装
  4. 适当配置覆盖率阈值
  5. 创建强制执行覆盖率的预提交钩子
  6. 提供必要的故障排除指导