All skills
antfu avatar

/vitest

@d02c484 official
by Anthony Fuantfu/skills5.9k stars
335

Vitest 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.

Use this Skill: https://skilld.dev/gh/antfu/skills/vitest

This session only. Nothing lands on disk.

referencesfeatures-coverage.md

≈1.3k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Code Coverage

Setup

# Run tests with coverage
vitest run --coverage

Configuration

// 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,
      },

      // v5 (v8 only): also cover node:child_process / node:worker_threads
      // spawned during the run (adds overhead via NODE_V8_COVERAGE)
      autoAttachSubprocess: false,
    },
  },
})

Providers

V8 (Default)

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

npm i -D @vitest/coverage-istanbul
  • Pre-instruments code
  • Works in any JS runtime
  • More overhead but widely compatible

Reporters

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:

coverage: {
  thresholds: {
    // Global thresholds
    lines: 80,
    functions: 75,
    branches: 70,
    statements: 80,
    
    // Per-file thresholds
    perFile: true,
    
    // Auto-update thresholds (for gradual improvement)
    // v5: a function receives (newThreshold, previousThreshold)
    autoUpdate: true,

    // v5: glob thresholds no longer inherit top-level `perFile` — set it per glob
    'src/utils/**': { lines: 80, perFile: true },
  },
}

Ignoring Code

V8

/* v8 ignore next -- @preserve */
function ignored() {
  return 'not covered'
}

/* v8 ignore start -- @preserve */
// All code here ignored
/* v8 ignore stop -- @preserve */

Istanbul

/* istanbul ignore next -- @preserve */
function ignored() {}

/* istanbul ignore if -- @preserve */
if (condition) {
  // ignored
}

Note: @preserve keeps comments through esbuild.

Package.json Scripts

{
  "scripts": {
    "test": "vitest",
    "test:coverage": "vitest run --coverage",
    "test:coverage:watch": "vitest --coverage"
  }
}

Vitest UI Coverage

Enable HTML coverage in Vitest UI:

coverage: {
  enabled: true,
  reporter: ['text', 'html'],
}

Run with vitest --ui to view coverage visually.

CI Integration

# 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/):

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

v5 Changes

  • include/exclude match relative paths (not absolute-with-contains), so patterns catch fewer files than v4 — a wildcard-free pattern like 'src' means src/**. Re-verify the reported file set after upgrading.
  • Glob thresholds don't inherit top-level perFile — set perFile on each glob that needs it.
  • coverage.autoAttachSubprocess (v8) tracks child-process/worker-thread coverage.
  • Istanbul moved to the maintained @vitest/istanbuljs fork; the v8 provider merges reports with bounded memory.

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

Source: SKILL.md on GitHub

No alerts3d5 checks · Risk SAFE
  • Gen Agent Trust Hub3d

    The skill provides comprehensive documentation and reference material for the Vitest testing framework. It covers configuration, CLI usage, mocking, coverage, and advanced features like type testing and benchmarking. No security issues were detected; the instructions follow standard development and testing practices.

  • Socket3d

    No alerts

  • Snyk3d

    Risk: LOW · No issues

  • Runlayer7mo

    1/18 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at d02c484. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Activeupdated 4 days ago
Other metadata
metadata
{
  "author": "Anthony Fu",
  "version": "2026.9.25",
  "source": "Generated from https://github.com/vitest-dev/vitest, scripts located at https://github.com/antfu/skills"
}

README badge

README badge for antfu/skills/vitest

Vitest is a Jest-compatible unit testing framework powered by Vite, with native ESM, TypeScript, and JSX support. Use it for writing tests with smart watch mode, mocking, fixtures, coverage reporting, and snapshot testing in projects already using Vite.

Generated from the current SKILL.md.

Is Vitest a drop-in replacement for Jest?
Mostly. Vitest provides a Jest-compatible API and can run most Jest test suites without modification, but it uses Vite's transformation pipeline instead of Jest's, so some edge cases may differ.
Does Vitest require configuration?
No. It natively supports ESM, TypeScript, and JSX out of the box and shares Vite's config, transformers, and resolvers, so many projects need minimal or no additional setup.
What coverage providers does Vitest support?
Vitest includes built-in coverage via V8 or Istanbul providers.
Can I run tests in parallel?
Yes. Vitest uses multi-threaded workers for parallel test execution and supports sharding across multiple processes.
What test environments are available?
Vitest supports node, jsdom, happy-dom, and custom environments.

Generated from the current SKILL.md. These answers refresh after source changes.