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 + coverage

Config: 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.all was removed in Vitest 4 — set coverage.include explicitly 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

ReporterOutput
textTerminal table summary
text-summarySingle-line summary
htmlInteractive HTML report in coverage/
lcovLCOV file for CI tools (Codecov, SonarQube)
jsonJSON for programmatic consumption
json-summarySummary JSON
coberturaXML (Jenkins)
teamcityTeamCity service messages
cloverClover 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.info

Coverage Metric Definitions

MetricMeasures
StatementsEach statement executed
BranchesEach branch of if/switch/ternary executed
FunctionsEach function called
LinesEach source line executed

100% line coverage does not mean 100% branch coverage. A line with x ? a : b has 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'],
}