husky-test-coverage
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseHusky 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:
-
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 in dependencies, look for
jestorjest.config.jsjest.config.json - Vitest: Check for in dependencies, look for
vitestorvitest.config.tsvitest.config.js - Mocha: Check for in dependencies, check for coverage tool (nyc, c8)
mocha
- Jest: Check for
-
Check Coverage Configuration:
- Jest: Look for in jest.config.*
coverageThreshold - Vitest: Look for in vitest.config.*
coverage.thresholds - Mocha: Look for or coverage config in package.json
.nycrc.json
- Jest: Look for
-
Verify Existing Husky Setup:
- Check if directory exists
.husky/ - Review existing pre-commit hook
- Check if Husky is in package.json dependencies
- Check if
-
Detect Test Files:
- Scan for or
*.test.*files*.spec.* - Verify tests exist before enforcing coverage
- Scan for
在设置测试覆盖率之前,先探测项目的上下文:
-
检查package.json:
- 查看现有测试脚本
- 从依赖项中检测测试运行器(jest、vitest、mocha)
- 检查是否已安装Husky
- 查看现有覆盖率配置
-
识别测试运行器:
- Jest:检查依赖项中是否有,查找
jest或jest.config.jsjest.config.json - Vitest:检查依赖项中是否有,查找
vitest或vitest.config.tsvitest.config.js - Mocha:检查依赖项中是否有,检查覆盖率工具(nyc、c8)
mocha
- Jest:检查依赖项中是否有
-
检查覆盖率配置:
- Jest:在jest.config.*中查找
coverageThreshold - Vitest:在vitest.config.*中查找
coverage.thresholds - Mocha:查找或package.json中的覆盖率配置
.nycrc.json
- Jest:在jest.config.*中查找
-
验证现有Husky设置:
- 检查是否存在目录
.husky/ - 查看现有预提交钩子
- 检查package.json依赖项中是否有Husky
- 检查是否存在
-
检测测试文件:
- 扫描或
*.test.*文件*.spec.* - 在强制执行覆盖率之前验证测试是否存在
- 扫描
Quick Start
快速开始
bash
undefinedbash
undefinedBasic 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
--root /path/to/project
python3 ~/.claude/skills/husky-test-coverage/scripts/setup-husky-coverage.py
--root /path/to/project
--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
--root /path/to/project
--threshold 85
python3 ~/.claude/skills/husky-test-coverage/scripts/setup-husky-coverage.py
--root /path/to/project
--threshold 85
--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
--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
--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
--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
--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
--root /path/to/project
--dry-run
undefinedpython3 ~/.claude/skills/husky-test-coverage/scripts/setup-husky-coverage.py
--root /path/to/project
--dry-run
--root /path/to/project
--dry-run
undefinedWhat Gets Configured
配置内容
Husky Setup
Husky 设置
- Installs Husky if not already present
- Initializes Husky ()
npx husky install - Creates hook that runs tests with coverage
.husky/pre-commit - Adds script to package.json (if missing)
prepare
- 如果尚未安装则安装Husky
- 初始化Husky()
npx husky install - 创建钩子,运行带覆盖率的测试
.husky/pre-commit - 向package.json添加脚本(如果缺失)
prepare
Test Runner Detection
测试运行器检测
The skill automatically detects:
- Jest: Uses in pre-commit hook
jest --coverage --watchAll=false - Vitest: Uses in pre-commit hook
vitest --coverage --run - Mocha: Uses or
nycwith mocha test commandc8
此技能会自动检测:
- Jest:在预提交钩子中使用
jest --coverage --watchAll=false - Vitest:在预提交钩子中使用
vitest --coverage --run - Mocha:将或
nyc与mocha测试命令配合使用c8
Coverage Configuration
覆盖率配置
Jest:
- Creates or updates with
jest.config.jsoncoverageThreshold - Default thresholds: 80% lines, 75% branches, 80% functions, 80% statements
Vitest:
- Creates or updates with coverage thresholds
vitest.config.ts/js - Configures v8 coverage provider
- Sets same default thresholds as Jest
Mocha + nyc:
- Creates or updates with coverage thresholds
.nycrc.json - Configures text, html, and lcov reporters
Jest:
- 创建或更新,添加
jest.config.jsoncoverageThreshold - 默认阈值: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
命令行参数
- : Project root directory (required)
--root <path> - : Coverage threshold percentage (default: 80)
--threshold <number> - : Fail commit if coverage below threshold (default: true)
--fail-on-below - : Allow commit even if coverage below threshold
--no-fail-on-below - : Skip hook if no test files found
--skip-if-no-tests - : Show what would be done without making changes
--dry-run
- :项目根目录(必填)
--root <path> - :覆盖率阈值百分比(默认:80)
--threshold <number> - :如果覆盖率低于阈值则阻止提交(默认:true)
--fail-on-below - :即使覆盖率低于阈值也允许提交
--no-fail-on-below - :如果无测试文件则跳过钩子
--skip-if-no-tests - :显示将执行的操作但不进行实际更改
--dry-run
Configuration File
配置文件
Create in project root:
.husky-test-coverage.jsonjson
{
"coverageThreshold": {
"lines": 80,
"branches": 75,
"functions": 80,
"statements": 80
},
"failOnCoverageBelowThreshold": true,
"skipIfNoTests": false
}在项目根目录创建:
.husky-test-coverage.jsonjson
{
"coverageThreshold": {
"lines": 80,
"branches": 75,
"functions": 80,
"statements": 80
},
"failOnCoverageBelowThreshold": true,
"skipIfNoTests": false
}Package.json Configuration
Package.json 配置
Alternatively, add to :
package.jsonjson
{
"huskyTestCoverage": {
"threshold": 80,
"failOnBelow": true
}
}或者,添加到:
package.jsonjson
{
"huskyTestCoverage": {
"threshold": 80,
"failOnBelow": true
}
}Tech Stack Adaptation
技术栈适配
Jest Projects
Jest 项目
Detection:
- Checks for in dependencies
jest - Looks for or
jest.config.jsjest.config.json
Configuration:
- Updates or creates with coverage thresholds
jest.config.json - 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.jsjest.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 in dependencies
vitest - Looks for or
vitest.config.tsvitest.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.tsvitest.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 in dependencies
mocha - Checks for coverage tool (or
nyc)c8
Configuration:
- Creates or updates for nyc
.nycrc.json - 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:
-
Discover Project Context:
- Scan package.json for test runner and dependencies
- Check existing Husky configuration
- Review existing coverage config files
- Verify test files exist
-
Detect Test Runner:
- Identify Jest, Vitest, or Mocha
- Detect coverage tool (built-in or nyc/c8)
- Determine package manager
-
Setup or Verify Husky:
- Install Husky if missing
- Initialize Husky hooks
- Add prepare script if needed
-
Configure Coverage:
- Create or update coverage configuration
- Set coverage thresholds (default 80%)
- Configure appropriate reporters
-
Create Pre-commit Hook:
- Generate hook script with test command
- Configure to run tests with coverage
- Set enforcement behavior (block or warn)
-
Verify Setup:
- Review generated configuration
- Test hook with a commit
- Adjust thresholds if needed
使用此技能时:
-
探测项目上下文:
- 扫描package.json以获取测试运行器和依赖项
- 检查现有Husky配置
- 查看现有覆盖率配置文件
- 验证测试文件是否存在
-
检测测试运行器:
- 识别Jest、Vitest或Mocha
- 检测覆盖率工具(内置或nyc/c8)
- 确定包管理器
-
设置或验证Husky:
- 如果缺失则安装Husky
- 初始化Husky钩子
- 必要时添加prepare脚本
-
配置覆盖率:
- 创建或更新覆盖率配置
- 设置覆盖率阈值(默认80%)
- 配置合适的报告器
-
创建预提交钩子:
- 生成带测试命令的钩子脚本
- 配置为运行带覆盖率的测试
- 设置强制执行行为(阻止或警告)
-
验证设置:
- 查看生成的配置
- 通过提交测试钩子
- 必要时调整阈值
Integration with Other Skills
与其他技能的集成
This skill works alongside:
| Skill | How It Works Together |
|---|---|
| fullstack-workspace-init | Automatically invoked after scaffolding to set up 80% coverage threshold |
| linter-formatter-init | Both configure Husky; this skill focuses on test coverage, linter-formatter-init focuses on linting/formatting |
| testing-expert | Uses 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 to scaffold a new project, this skill is automatically applied with:
fullstack-workspace-init- 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-initManual 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 80Troubleshooting
故障排除
Pre-commit hook not running
预提交钩子未运行
bash
undefinedbash
undefinedReinstall Husky
Reinstall Husky
npx husky install
chmod +x .husky/pre-commit
undefinednpx husky install
chmod +x .husky/pre-commit
undefinedCoverage 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
最佳实践
- Start with reasonable thresholds: Begin with 80% and adjust based on project needs
- Monitor coverage trends: Use coverage reports to identify gaps
- Incremental improvement: Gradually increase thresholds as coverage improves
- Consider context: Some files (utilities, configs) may not need high coverage
- Use with CI/CD: Pre-commit hooks catch issues early, but CI/CD provides final gate
- Team alignment: Ensure team understands coverage requirements and goals
- 从合理的阈值开始: 从80%开始,根据项目需求调整
- 监控覆盖率趋势: 使用覆盖率报告识别差距
- 逐步改进: 随着覆盖率提高逐渐增加阈值
- 考虑上下文: 某些文件(工具类、配置文件)可能不需要高覆盖率
- 与CI/CD配合使用: 预提交钩子提前发现问题,但CI/CD提供最终保障
- 团队对齐: 确保团队了解覆盖率要求和目标
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
When this skill is active, Claude will:
- Discover project test setup and configuration
- Detect test runner and coverage tool
- Set up or verify Husky installation
- Configure coverage thresholds appropriately
- Create pre-commit hook that enforces coverage
- Provide troubleshooting guidance when needed
- Husky 文档:https://typicode.github.io/husky/
- Jest 覆盖率:https://jestjs.io/docs/configuration#coveragethreshold-object
- Vitest 覆盖率:https://vitest.dev/config/#coverage
- nyc (Istanbul):https://github.com/istanbuljs/nyc
当此技能激活时,Claude会:
- 探测项目测试设置和配置
- 检测测试运行器和覆盖率工具
- 设置或验证Husky安装
- 适当配置覆盖率阈值
- 创建强制执行覆盖率的预提交钩子
- 提供必要的故障排除指导