gentleeduck/duck-ui / .claude/skills/vitest
vitest
VerifiedVitest fast unit testing framework powered by Vite with Jest-compatible API. Use when writing tests, mocking, configuring coverage, or working with test filtering and fixtures.
| File | Kind | Size |
|---|---|---|
| GENERATION.md | markdown | 136 B |
| SKILL.md | skill md | 3.6 KB |
| agents/openai.yaml | config | 98 B |
| references/advanced-environments.md | markdown | 4.7 KB |
| references/advanced-projects.md | markdown | 5.4 KB |
| references/advanced-type-testing.md | markdown | 4.6 KB |
| references/advanced-vi.md | markdown | 5.6 KB |
| references/core-cli.md | markdown | 4.1 KB |
| references/core-config.md | markdown | 4.7 KB |
| references/core-describe.md | markdown | 3.5 KB |
| references/core-expect.md | markdown | 6.6 KB |
| references/core-hooks.md | markdown | 4.8 KB |
| references/core-test-api.md | markdown | 5.3 KB |
| references/features-benchmarking.md | markdown | 3.6 KB |
| references/features-concurrency.md | markdown | 4.7 KB |
| references/features-context.md | markdown | 5.3 KB |
| references/features-coverage.md | markdown | 4.2 KB |
| references/features-filtering.md | markdown | 3.8 KB |
| references/features-mocking.md | markdown | 6.9 KB |
| references/features-reporters.md | markdown | 3.7 KB |
| references/features-snapshots.md | markdown | 5.2 KB |
| references/features-test-tags.md | markdown | 3.6 KB |
references/features-coverage.md
View on GitHub---
name: code-coverage
description: Code coverage with V8 or Istanbul providers
---
# Code Coverage
## Setup
```bash
# Run tests with coverage
vitest run --coverage
```
## Configuration
```ts
// 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,
},
},
},
})
```
## Providers
### V8 (Default)
```bash
npm i -D @vitest/coverage-v8
```
- Faster, no pre-instrumentation
- Uses V8's native coverage
- v4 uses **AST-based remapping** (as accurate as Istanbul); expect coverage numbers to shift when upgrading from v3
- Recommended for most projects
### Istanbul
```bash
npm i -D @vitest/coverage-istanbul
```
- Pre-instruments code
- Works in any JS runtime
- More overhead but widely compatible
## Reporters
```ts
coverage: {
reporter: [
'text', // Terminal output
'text-summary', // Summary only
'json', // JSON file
'html', // HTML report
'lcov', // For CI tools
'cobertura', // XML format
],
reportsDirectory: './coverage',
}
```
## Thresholds
Fail tests if coverage is below threshold:
```ts
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 },
},
}
```
## Ignoring Code
### V8
```ts
/* v8 ignore next -- @preserve */
function ignored() {
return 'not covered'
}
/* v8 ignore start -- @preserve */
// All code here ignored
/* v8 ignore stop -- @preserve */
```
### Istanbul
```ts
/* istanbul ignore next -- @preserve */
function ignored() {}
/* istanbul ignore if -- @preserve */
if (condition) {
// ignored
}
```
Note: `@preserve` keeps comments through esbuild.
## Package.json Scripts
```json
{
"scripts": {
"test": "vitest",
"test:coverage": "vitest run --coverage",
"test:coverage:watch": "vitest --coverage"
}
}
```
## Vitest UI Coverage
Enable HTML coverage in Vitest UI:
```ts
coverage: {
enabled: true,
reporter: ['text', 'html'],
}
```
Run with `vitest --ui` to view coverage visually.
## CI Integration
```yaml
# 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.info
```
## Coverage with Sharding
Merge coverage from sharded runs (blobs default to `.vitest/blob/`):
```bash
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=json
```
## v4 Changes
- **`coverage.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.
- Programmatic coverage APIs moved from `vitest/coverage` to `vitest/node`.
## Key Points
- V8 is faster, Istanbul is more compatible
- Use `--coverage` flag or `coverage.enabled: true`
- Define `coverage.include` to report uncovered source files
- Set thresholds to enforce minimum coverage
- Use `@preserve` comment to keep ignore hints (e.g. `/* v8 ignore next -- @preserve */`)
<!--
Source references:
- https://vitest.dev/guide/coverage.html
-->
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |