Setting the file. One moment.
Subchapter 13.14
references/features-coverage.mdMarkdown4 KBView on GitHub
# Run tests with coverage
vitest run --coverage// vitest.config.ts
defineConfig({
test: {
coverage: {
// Provider: 'v8' (default, faster) or 'istanbul' (more compatible)
provider: 'v8',
// Enable coverage
enabled: true,
// Reporters
reporter: ['text', 'json', 'html'],
// v4: define `include` to report uncovered files too.
// Without it, only files loaded during the run are reported.
include: ['src/**/*.{ts,tsx}'],
// Exclusion is applied to files matched by `include`
exclude: [
'**/*.d.ts',
'**/*.test.ts',
],
// Thresholds
thresholds: {
lines: 80,
functions: 80,
branches: 80,
statements: 80,
},
},
},
})npm i -D @vitest/coverage-v8npm i -D @vitest/coverage-istanbulcoverage: {
reporter: [
'text', // Terminal output
'text-summary', // Summary only
'json', // JSON file
'html', // HTML report
'lcov', // For CI tools
'cobertura', // XML format
],
reportsDirectory: './coverage',
}Fail tests if coverage is below threshold:
coverage: {
thresholds: {
// Global thresholds
lines: 80,
functions: 75,
branches: 70,
statements: 80,
// Per-file thresholds
perFile: true,
// Auto-update thresholds (for gradual improvement)
autoUpdate: true,
// v5: glob thresholds no longer inherit top-level `perFile` — set it per glob
'src/utils/**': { lines: 80, perFile: true },
},
}/* v8 ignore next -- @preserve */
function ignored() {
return 'not covered'
}
/* v8 ignore start -- @preserve */
// All code here ignored
/* v8 ignore stop -- @preserve *//* istanbul ignore next -- @preserve */
function ignored() {}
/* istanbul ignore if -- @preserve */
if (condition) {
// ignored
}Note: @preserve keeps comments through esbuild.
{
"scripts": {
"test": "vitest",
"test:coverage": "vitest run --coverage",
"test:coverage:watch": "vitest --coverage"
}
}Enable HTML coverage in Vitest UI:
coverage: {
enabled: true,
reporter: ['text', 'html'],
}Run with vitest --ui to view coverage visually.
# GitHub Actions
- name: Run tests with coverage
run: npm run test:coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
files: ./coverage/lcov.infoMerge coverage from sharded runs (blobs default to .vitest/blob/):
vitest run --shard=1/3 --coverage --reporter=blob
vitest run --shard=2/3 --coverage --reporter=blob
vitest run --shard=3/3 --coverage --reporter=blob
vitest --merge-reports --coverage --reporter=jsoncoverage.all and coverage.extensions removed — only covered files are reported unless coverage.include is set.coverage.ignoreEmptyLines removed; lines without runtime code are no longer counted.coverage.experimentalAstAwareRemapping removed — AST remapping is the default and only mode for V8.vitest/coverage to vitest/node.--coverage flag or coverage.enabled: truecoverage.include to report uncovered source files@preserve comment to keep ignore hints (e.g. /* v8 ignore next -- @preserve */)