Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vitest 3.x reference skill covering configuration, test/describe APIs, mocking, coverage, snapshots, and concurrency.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/features-coverage.md
1---2name: code-coverage3description: Code coverage with V8 or Istanbul providers4---56# Code Coverage78## Setup910```bash11# Run tests with coverage12vitest run --coverage13```1415## Configuration1617```ts18// vitest.config.ts19defineConfig({20test: {21coverage: {22// Provider: 'v8' (default, faster) or 'istanbul' (more compatible)23provider: 'v8',2425// Enable coverage26enabled: true,2728// Reporters29reporter: ['text', 'json', 'html'],3031// v4: define `include` to report uncovered files too.32// Without it, only files loaded during the run are reported.33include: ['src/**/*.{ts,tsx}'],3435// Exclusion is applied to files matched by `include`36exclude: [37'**/*.d.ts',38'**/*.test.ts',39],4041// Thresholds42thresholds: {43lines: 80,44functions: 80,45branches: 80,46statements: 80,47},48},49},50})51```5253## Providers5455### V8 (Default)5657```bash58npm i -D @vitest/coverage-v859```6061- Faster, no pre-instrumentation62- Uses V8's native coverage63- v4 uses **AST-based remapping** (as accurate as Istanbul); expect coverage numbers to shift when upgrading from v364- Recommended for most projects6566### Istanbul6768```bash69npm i -D @vitest/coverage-istanbul70```7172- Pre-instruments code73- Works in any JS runtime74- More overhead but widely compatible7576## Reporters7778```ts79coverage: {80reporter: [81'text', // Terminal output82'text-summary', // Summary only83'json', // JSON file84'html', // HTML report85'lcov', // For CI tools86'cobertura', // XML format87],88reportsDirectory: './coverage',89}90```9192## Thresholds9394Fail tests if coverage is below threshold:9596```ts97coverage: {98thresholds: {99// Global thresholds100lines: 80,101functions: 75,102branches: 70,103statements: 80,104105// Per-file thresholds106perFile: true,107108// Auto-update thresholds (for gradual improvement)109autoUpdate: true,110111// v5: glob thresholds no longer inherit top-level `perFile` — set it per glob112'src/utils/**': { lines: 80, perFile: true },113},114}115```116117## Ignoring Code118119### V8120121```ts122/* v8 ignore next -- @preserve */123function ignored() {124return 'not covered'125}126127/* v8 ignore start -- @preserve */128// All code here ignored129/* v8 ignore stop -- @preserve */130```131132### Istanbul133134```ts135/* istanbul ignore next -- @preserve */136function ignored() {}137138/* istanbul ignore if -- @preserve */139if (condition) {140// ignored141}142```143144Note: `@preserve` keeps comments through esbuild.145146## Package.json Scripts147148```json149{150"scripts": {151"test": "vitest",152"test:coverage": "vitest run --coverage",153"test:coverage:watch": "vitest --coverage"154}155}156```157158## Vitest UI Coverage159160Enable HTML coverage in Vitest UI:161162```ts163coverage: {164enabled: true,165reporter: ['text', 'html'],166}167```168169Run with `vitest --ui` to view coverage visually.170171## CI Integration172173```yaml174# GitHub Actions175- name: Run tests with coverage176run: npm run test:coverage177178- name: Upload coverage to Codecov179uses: codecov/codecov-action@v3180with:181files: ./coverage/lcov.info182```183184## Coverage with Sharding185186Merge coverage from sharded runs (blobs default to `.vitest/blob/`):187188```bash189vitest run --shard=1/3 --coverage --reporter=blob190vitest run --shard=2/3 --coverage --reporter=blob191vitest run --shard=3/3 --coverage --reporter=blob192193vitest --merge-reports --coverage --reporter=json194```195196## v4 Changes197198- **`coverage.all` and `coverage.extensions` removed** — only covered files are reported unless `coverage.include` is set.199- **`coverage.ignoreEmptyLines` removed**; lines without runtime code are no longer counted.200- **`coverage.experimentalAstAwareRemapping` removed** — AST remapping is the default and only mode for V8.201- Programmatic coverage APIs moved from `vitest/coverage` to `vitest/node`.202203## Key Points204205- V8 is faster, Istanbul is more compatible206- Use `--coverage` flag or `coverage.enabled: true`207- Define `coverage.include` to report uncovered source files208- Set thresholds to enforce minimum coverage209- Use `@preserve` comment to keep ignore hints (e.g. `/* v8 ignore next -- @preserve */`)210211<!--212Source references:213- https://vitest.dev/guide/coverage.html214-->215