by thetestingacademy
Track test coverage with Istanbul/nyc, generate reports, and enforce thresholds
npx @qaskills/cli add code-coverageAuto-detects your AI agent and installs the skill. Works with Claude Code, Cursor, Copilot, and more.
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.
if (a && b) return x; can be 100 percent line-covered with three of its four branch outcomes untested.coverageThreshold (Jest), thresholds (Vitest), or --check-coverage (nyc/c8) so CI goes red on regression.all files, not just imported ones. By default some tools only report files touched by tests, so a completely untested module is invisible. Enable all: true (nyc), coverage.all (Vitest), or a broad collectCoverageFrom (Jest).*.d.ts inflate or deflate numbers randomly. Exclude them in config, never by sprinkling ignore comments through generated files.// 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
{
"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
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/
// 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
// 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.
// Istanbul-instrumented runners (nyc, babel-plugin-istanbul)
/* istanbul ignore next -- @preserve defensive guard, unreachable after zod validation */
if (typeof input !== 'string') throw new TypeError('input must be a string');
// V8-based runners (vitest --coverage.provider=v8, c8)
/* v8 ignore next 2 */
if (process.platform === 'win32') {
pathSeparator = '\\';
}
Every ignore comment needs a reason suffix; an ignore without a justification is a coverage lie waiting to rot.
# .github/workflows/test.yml (excerpt)
- name: Test with coverage gate
run: npx vitest run --coverage
- name: Print coverage summary to job log
if: always()
run: |
pct_lines=$(jq -r '.total.lines.pct' coverage/coverage-summary.json)
pct_branches=$(jq -r '.total.branches.pct' coverage/coverage-summary.json)
echo "### Coverage: ${pct_lines}% lines / ${pct_branches}% branches" >> "$GITHUB_STEP_SUMMARY"
- name: Upload lcov for review tooling
uses: actions/upload-artifact@v4
with:
name: lcov-report
path: coverage/lcov.info
coverage/index.html) when writing tests for legacy code; the red/yellow highlighting finds untested branches faster than reading source.json-summary totals for speed, keep lcov output for editors and review tools that show per-line coverage in diffs.istanbul ignore on reachable code is technical debt with a receipt.nyc merge, --coverage.reportsDirectory per suite plus lcov merge) before judging.coverage/ get committed; add it to .gitignore.--coverage, coverageThreshold, .nycrc, c8, or @vitest/coverage-v8 to the project.- name: Install QA Skills
run: npx @qaskills/cli add code-coverage10 of 29 agents supported
Build AI agents that write, run, and fix tests. Playwright, LLM evals, and CI in one live cohort.
Use code AITESTER at checkout