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// Files to include32include: ['src/**/*.{ts,tsx}'],3334// Files to exclude35exclude: [36'node_modules/',37'tests/',38'**/*.d.ts',39'**/*.test.ts',40],4142// Report uncovered files43all: true,4445// Thresholds46thresholds: {47lines: 80,48functions: 80,49branches: 80,50statements: 80,51},52},53},54})55```5657## Providers5859### V8 (Default)6061```bash62npm i -D @vitest/coverage-v863```6465- Faster, no pre-instrumentation66- Uses V8's native coverage67- Recommended for most projects6869### Istanbul7071```bash72npm i -D @vitest/coverage-istanbul73```7475- Pre-instruments code76- Works in any JS runtime77- More overhead but widely compatible7879## Reporters8081```ts82coverage: {83reporter: [84'text', // Terminal output85'text-summary', // Summary only86'json', // JSON file87'html', // HTML report88'lcov', // For CI tools89'cobertura', // XML format90],91reportsDirectory: './coverage',92}93```9495## Thresholds9697Fail tests if coverage is below threshold:9899```ts100coverage: {101thresholds: {102// Global thresholds103lines: 80,104functions: 75,105branches: 70,106statements: 80,107108// Per-file thresholds109perFile: true,110111// Auto-update thresholds (for gradual improvement)112autoUpdate: 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: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## Key Points197198- V8 is faster, Istanbul is more compatible199- Use `--coverage` flag or `coverage.enabled: true`200- Include `all: true` to see uncovered files201- Set thresholds to enforce minimum coverage202- Use `@preserve` comment to keep ignore hints203204<!--205Source references:206- https://vitest.dev/guide/coverage.html207-->208