Agent skill
nestjs-vitest-coverage
Upgrade, repair, and enforce Vitest coverage in NestJS backends with deterministic provider alignment, coverage config hardening, script normalization, threshold enforcement, and test-gap remediation. Use when users ask to fix failing coverage, migrate or change coverage provider (v8/istanbul), configure coverage reporters/thresholds, raise CI coverage, or stop coverage drift after framework/test upgrades.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/nestjs-vitest-coverage
SKILL.md
NestJS Vitest Coverage
Use this workflow to keep NestJS coverage reliable, enforceable in CI, and aligned with current Vitest behavior.
1. Preflight and baseline audit
- Detect package manager from lockfiles and use matching commands (
npm,pnpm, oryarn) for all install and script operations. - Verify runtime and toolchain:
node -v- package manager version
- Verify current versions and coverage package alignment:
npm ls vitest @vitest/coverage-v8 @vitest/coverage-istanbul
- Confirm where test config lives:
vitest.config.ts, orvite.config.tswith atestblock.
- Check whether
package.jsonhastest:covand whether coverage output is cleaned. - Run baseline coverage once and capture current failures before editing:
npm run test:cov(or package-manager equivalent).
2. Upgrade packages
Prefer v8 coverage unless the project explicitly requires Istanbul compatibility.
v8 provider:
npm install -D vitest@latest @vitest/coverage-v8@latest
istanbul provider:
npm install -D vitest@latest @vitest/coverage-istanbul@latest
Rules:
- Keep
vitestand selected coverage package on the same major version. - Install exactly one provider package unless the project intentionally keeps both.
- If both providers are installed unintentionally, remove the unused one.
3. Configure coverage
Add or update the coverage block in the active Vitest config:
coverage: {
provider: 'v8',
reporter: ['text', 'html', 'lcov'],
reportsDirectory: 'coverage',
include: ['src/**/*.ts'],
exclude: [
'node_modules',
'dist',
'**/*.spec.ts',
'**/*.test.ts',
'src/main.ts',
'**/*.module.ts',
'test/**',
],
thresholds: {
lines: 85,
functions: 85,
branches: 80,
statements: 85,
},
},
Guidelines:
- Set
providerto match the installed package. - Keep
textfor terminal feedback andhtmlfor local inspection. - Add
lcovfor CI tools such as Sonar and Codecov. - Use
includeto measure real source files instead of only executed files. - Exclude only entrypoints, modules, test files, generated code, and tooling.
- Keep or increase existing thresholds; do not reduce thresholds unless user explicitly requests it.
4. Ensure scripts are correct
Update package.json scripts:
{
"scripts": {
"test": "vitest run",
"test:watch": "vitest",
"test:cov": "vitest run --coverage",
"test:cov:watch": "vitest --coverage --watch"
}
}
If a cleanup script exists, ensure coverage output is removed (for example rimraf dist coverage .cache).
5. Run and inspect coverage
- Run:
npm run test:cov
- Inspect:
- Terminal summary for low files and uncovered lines.
coverage/index.htmlfor branch-level gaps.
- Confirm thresholds are enforced and fail below target.
- If CI runs coverage, confirm command parity between local and CI scripts/workflows.
6. Raise coverage by improving tests
Preferred approach: add or improve tests for uncovered project code.
For NestJS:
- Use
Test.createTestingModuleand mock dependencies withvi.fn. - Add branch tests for success, failure, and edge paths.
- Cover guards, pipes, interceptors, and service error handling.
- In e2e suites, always close the app in
afterAll. - Prefer deterministic mocks/spies and avoid flaky time/network coupling.
Do not make coverage pass by excluding maintained source files.
7. Apply exclusions responsibly
Allowed exclusions:
- Generated clients (for example Prisma generated output)
- Framework entrypoints (
src/main.ts) - Thin wiring modules (
**/*.module.ts) - Test/setup files
Avoid excluding controllers, services, handlers, domain logic, and shared libraries.
8. Validate completion
- Coverage command passes locally with enforced thresholds
- Coverage directory includes expected reporters (
text,html,lcov) - Provider package matches config (
v8oristanbul) - CI coverage command is aligned with local script behavior
- Coverage gains came from test additions or fixes, not broad source exclusions
Additional resources
- reference.md - quick config and troubleshooting reference
- ../nestjs-vitest-setup/SKILL.md - full NestJS Vitest setup and migration
- Vitest coverage docs: https://vitest.dev/guide/coverage.html
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?