Code Coverage Analysis
FreeNo executable scriptsNot checkedMeasure and enforce test coverage with Istanbul/nyc, c8, Jest, and Vitest. Covers branch versus line coverage, per-directory thresholds, CI gates, and…
by PramodDutta★ 253 GitHub starsAbout this skill
Code Coverage Analysis
This skill makes an AI agent configure coverage collection correctly (Istanbul instrumentation or V8 native coverage), set thresholds that fail builds, read coverage reports to find genuinely untested branches, and exclude generated or config code so the numbers mean something. Trigger it when a user asks "what is our coverage", wants a coverage gate in CI, or when a coverage/ directory, --coverage flag, .nycrc, or coverageThreshold appears in the project.
Core Principles
- Branch coverage is the number that matters. Line coverage marks a line as hit even if only one of its outcomes ran. A guard clause
if (a && b) return x;can be 100 percent line-covered with three of its four branch outcomes untested. - Coverage is a gap detector, not a quality score. 95 percent coverage with assertion-free tests proves nothing. Use coverage to find untested code paths; use mutation testing to check assertion strength.
- Thresholds must fail the build. A coverage report nobody reads is decoration. Wire
coverageThreshold(Jest),thresholds(Vitest), or--check-coverage(nyc/c8) so CI goes red on regression. - Set thresholds at current reality, then ratchet. Dropping a 90 percent global gate onto a 60 percent codebase makes the team delete the gate. Start at today's number minus 1, raise it as coverage improves.
- Measure
allfiles, not just imported ones. By default some tools only report files touched by tests, so a completely untested module is invisible. Enableall: true(nyc),coverage.all(Vitest), or a broadcollectCoverageFrom(Jest). - Exclude generated code explicitly. Protobuf stubs, GraphQL codegen, migrations, and
*.d.tsinflate or deflate numbers randomly. Exclude them in config, never by sprinkling ignore comments through generated files.
Setup and Patterns
1. Jest: collection, thresholds, and per-directory gates
// jest.config.js
module.exports = {
preset: 'ts-jest',
collectCoverage: true,
coverageProvider: 'v8',
collectCoverageFrom: [
'src/**/*.{ts,tsx}',
'!src/**/*.d.ts',
'!src/**/__generated__/**',
'!src/**/*.stories.tsx',
'!src/test-utils/**',
],
coverageReporters: ['text', 'lcov', 'json-summary'],
coverageThreshold: {
global: {
branches: 80,
functions: 85,
lines: 90,
statements: 90,
},
// Money-handling code gets a stricter gate
'./src/lib/payments/': {
branches: 95,
lines: 98,
},
},
};
npx jest --coverage --ci
# Exit code is 1 if any threshold is missed; CI fails automatically
2. nyc (Istanbul) for Mocha or plain Node scripts
{
"all": true,
"include": ["src/**/*.ts"],
"exclude": ["**/*.spec.ts", "src/generated/**", "src/migrations/**"],
"reporter": ["text", "html", "lcov"],
"check-coverage": true,
"branches": 80,
"lines": 90,
"functions": 85,
"statements": 90
}
Save as .nycrc.json, then:
npm install --save-dev nyc
npx nyc mocha 'test/**/*.spec.ts'
npx nyc report --reporter=text-summary
3. c8: native V8 coverage with zero instrumentation
c8 reads V8's built-in coverage, so it works with the Node test runner and needs no transpile-time instrumentation:
npm install --save-dev c8
npx c8 --all --src src --reporter=text --reporter=lcov \
--lines 90 --branches 80 --check-coverage \
node --test test/
4. Vitest: built-in V8 coverage with thresholds
// vitest.config.ts
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
coverage: {
provider: 'v8',
all: true,
include: ['src/**/*.ts'],
exclude: ['src/generated/**', 'src/**/*.test.ts', '**/*.config.ts'],
reporter: ['text', 'lcov', 'json-summary'],
thresholds: {
lines: 90,
branches: 80,
functions: 85,
statements: 90,
// Fail if coverage DROPS below auto-updated values
autoUpdate: false,
},
},
},
});
npx vitest run --coverage
5. Branch versus line coverage, concretely
// shipping.ts
export function shippingCost(country: string, total: number): number {
if (country === 'US' && total > 50) return 0;
return country === 'US' ? 5 : 15;
}
// shipping.test.ts -- this single test yields 100% LINE coverage
import { shippingCost } from './shipping';
it('ships free for large US orders', () => {
expect(shippingCost('US', 100)).toBe(0);
});
// But branch coverage shows: total <= 50 untested, non-US untested,
// the $5 domestic path untested. Three real behaviors have no test.
The branch report (text reporter prints % Branch per file, the HTML report highlights yellow I/E markers) is how you find these.
6. Ignoring unreachable code the honest way
// Istanbul-instrumented runners (nyc, babel-plugin-istanbul)
/* istanbul ignore next -- @preserve defensive guard, unreachable after zod validation *
Install Code Coverage Analysis in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
FAQ
What does the Code Coverage Analysis skill do?
Measure and enforce test coverage with Istanbul/nyc, c8, Jest, and Vitest. Covers branch versus line coverage, per-directory thresholds, CI gates, and correctly excluding generated code from reports.
How do I install the Code Coverage Analysis skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the Code Coverage Analysis skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
skill-creator
Create new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an…
by Anthropicdiscernment-nudge
After you give a substantive answer or draft that the user may act on — advice or recommendations, drafted artifacts such as goals, plans, pitches, proposals…
by Anthropicdata-consolidate
Joins the plant's scheduled exports and connector data into one working dataset — ERP items (NetSuite or Infor M3), sales orders, production work orders…
by Anthropicinspection-to-capa
Runs the quality chain end to end: writes the nonconformance against the requirement it violates, searches history at the moment of writing to say whether…
by Anthropic