Vitest Cheatsheet
Coverage
Use this Vitest reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Installing a Coverage Provider
# V8 (built into Node — fast, no extra setup) npm install -D @vitest/coverage-v8 # Istanbul (more accurate, slower) npm install -D @vitest/coverage-istanbul
Running Coverage
vitest run --coverage
vitest --coverage # watch mode + coverageConfig: coverage Block
// vitest.config.ts import { defineConfig } from 'vitest/config' export default defineConfig({ test: { coverage: { provider: 'v8', // 'istanbul' | 'v8' reporter: ['text', 'html', 'lcov', 'json'], reportsDirectory: './coverage', include: ['src/**/*.{ts,tsx}'], exclude: [ 'src/**/*.test.{ts,tsx}', 'src/**/*.spec.{ts,tsx}', 'src/test/**', 'src/types/**', ], clean: true, // clean before each run }, }, })
coverage.allwas removed in Vitest 4 — setcoverage.includeexplicitly to pull un-tested files into the report.
Coverage Thresholds
coverage: {
thresholds: {
lines: 80,
functions: 80,
branches: 70,
statements: 80,
// Per-file thresholds:
perFile: true,
// Fail build if below thresholds:
// (Vitest exits with code 1 if any threshold is unmet)
},
}Coverage Reporters
| Reporter | Output |
|---|---|
text | Terminal table summary |
text-summary | Single-line summary |
html | Interactive HTML report in coverage/ |
lcov | LCOV file for CI tools (Codecov, SonarQube) |
json | JSON for programmatic consumption |
json-summary | Summary JSON |
cobertura | XML (Jenkins) |
teamcity | TeamCity service messages |
clover | Clover XML |
reporter: ['text', 'html', 'lcov']
Excluding Code from Coverage
Istanbul ignore comments:
/* istanbul ignore next */ function debugOnly() { console.log('dev only') } /* istanbul ignore if */ if (process.env.DEBUG) { setup() } /* istanbul ignore else */ if (condition) { doThing() } else { /* ignored branch */ }
V8 ignore comments:
/* v8 ignore next */ /* v8 ignore next 3 */ // ignore next N lines /* v8 ignore start */ function notTested() { ... } /* v8 ignore stop */
Viewing the HTML Report
vitest run --coverage open coverage/index.html
Or use the Vitest UI which integrates coverage inline:
vitest --ui --coverage
Coverage in CI
# GitHub Actions example
- name: Run tests with coverage
run: npx vitest run --coverage
- name: Upload to Codecov
uses: codecov/codecov-action@v4
with:
files: ./coverage/lcov.infoCoverage Metric Definitions
| Metric | Measures |
|---|---|
| Statements | Each statement executed |
| Branches | Each branch of if/switch/ternary executed |
| Functions | Each function called |
| Lines | Each source line executed |
100% line coverage does not mean 100% branch coverage. A line with
x ? a : bhas two branches.
allBranches (Istanbul Only)
coverage: {
provider: 'istanbul',
// Istanbul tracks logical branches including optional chaining
}Source Maps
Both providers require source maps to map coverage back to TypeScript:
export default defineConfig({ test: { coverage: { provider: 'v8' }, }, // Vite generates source maps automatically in test mode })
.nycrc / c8 Migration
If migrating from Istanbul/nyc or c8:
// Replace .nycrc or c8 config with vitest.config.ts coverage block coverage: { provider: 'istanbul', // matches nyc behavior most closely include: ['src/**'], exclude: ['**/*.test.ts'], }