Skip to content

vitest

npm version License: MIT Node.js Vitest

> Base Vitest configuration for jmlweb projects. TypeScript support, coverage settings, and sensible defaults out of the box.

  • 🔧 TypeScript Support: Configured for TypeScript projects (type checking via CLI)
  • 📊 Coverage Configuration: Pre-configured with v8 provider and 80% coverage thresholds
  • 🎯 Sensible Defaults: Node.js environment, globals enabled, optimized test execution
  • Performance Optimized: Thread pool configuration for efficient parallel test execution
  • 🚀 Easy Extension: Flat config format for easy customization
  • 📦 Zero Config: Works out of the box with minimal setup
Terminal window
pnpm add -D @jmlweb/vitest-config vitest

> 💡 Upgrading from a previous version? See the Migration Guide for breaking changes and upgrade instructions.

Create a vitest.config.ts file in your project root:

import { defineConfig } from 'vitest/config';
import baseConfig from '@jmlweb/vitest-config';
export default defineConfig({
...baseConfig,
// Add your project-specific overrides here
});
vitest.config.ts
import { defineConfig } from 'vitest/config';
import baseConfig from '@jmlweb/vitest-config';
export default defineConfig({
...baseConfig,
});
vitest.config.ts
import { defineConfig } from 'vitest/config';
import baseConfig from '@jmlweb/vitest-config';
export default defineConfig({
...baseConfig,
test: {
...baseConfig.test,
// Override environment for browser testing
environment: 'jsdom',
// Custom test timeout
testTimeout: 10000,
// Custom coverage thresholds
coverage: {
...baseConfig.test?.coverage,
thresholds: {
lines: 90,
functions: 90,
branches: 85,
statements: 90,
},
},
},
});
vitest.config.ts
import { defineConfig } from 'vitest/config';
import baseConfig from '@jmlweb/vitest-config';
export default defineConfig({
...baseConfig,
test: {
...baseConfig.test,
environment: 'jsdom', // Use jsdom for React component testing
setupFiles: ['./src/test/setup.ts'], // Optional setup file
},
});
vitest.config.ts
import { defineConfig } from 'vitest/config';
import baseConfig from '@jmlweb/vitest-config';
export default defineConfig({
...baseConfig,
test: {
...baseConfig.test,
coverage: {
...baseConfig.test?.coverage,
thresholds: {
lines: 60,
functions: 60,
branches: 60,
statements: 60,
},
},
},
});

> Philosophy: Modern testing should be fast, have excellent DX, and enforce meaningful coverage without slowing down development.

This package provides a Vitest configuration optimized for speed and developer experience while maintaining high code quality standards. It leverages Vitest’s Vite-powered architecture for extremely fast test execution and hot module replacement during test development.

80% Coverage Thresholds: Balanced quality enforcement

  • Why: 80% coverage ensures most code is tested without creating unrealistic goals that lead to testing trivial code just to hit metrics. It’s high enough to catch bugs but low enough to remain practical for real-world development
  • Trade-off: Some projects need higher (90%+) or lower (60%) thresholds depending on criticality
  • When to override: Increase for safety-critical code, decrease for rapid prototyping or legacy code being brought under test

Global Test Functions (globals: true): Familiar testing API

  • Why: Enables the familiar describe, it, expect globals without imports, matching Jest’s API. This makes migration easier and reduces boilerplate in every test file. Most developers expect this API
  • Trade-off: Technically pollutes global scope, but this is standard in testing and expected behavior
  • When to override: If you prefer explicit imports (import { describe, it, expect } from 'vitest') for stricter code style

Thread Pool (pool: 'threads'): Parallel test execution

  • Why: Vitest’s thread pool runs tests in parallel using worker threads, dramatically speeding up large test suites. Much faster than Jest’s default process-based isolation while maintaining proper isolation
  • Trade-off: Minimal memory overhead from worker threads. But the speed improvement is significant
  • When to override: For tests with native modules that don’t work in workers - use forks pool instead

V8 Coverage Provider: Fast native coverage

  • Why: V8’s built-in coverage is significantly faster than instrumentation-based coverage (like Istanbul). For Vitest projects, it’s the best choice for speed while maintaining accuracy
  • Trade-off: Less configurable than Istanbul, but coverage accuracy is equivalent for modern code
  • When to override: Rarely needed - V8 coverage works well for all modern JavaScript/TypeScript
SettingValueDescription
globalstrueEnables global test functions (describe, it, expect)
environmentnodeNode.js environment by default
testTimeout5000Test timeout in milliseconds (5 seconds)
hookTimeout10000Hook timeout in milliseconds (10 seconds)
poolthreadsUse thread pool for parallel test execution
coverage.providerv8Coverage provider
coverage.thresholds.lines80Minimum line coverage
coverage.thresholds.functions80Minimum function coverage
coverage.thresholds.branches80Minimum branch coverage
coverage.thresholds.statements80Minimum statement coverage

The following patterns are excluded from coverage by default:

  • **/*.d.ts - TypeScript declaration files
  • **/*.config.{ts,js} - Configuration files
  • **/dist/** - Build output
  • **/build/** - Build directories
  • **/node_modules/** - Dependencies
  • **/coverage/** - Coverage reports
  • **/*.test.{ts,tsx,js,jsx} - Test files
  • **/*.spec.{ts,tsx,js,jsx} - Spec files

Tests are automatically discovered from:

  • **/*.test.{ts,tsx,js,jsx}
  • **/*.spec.{ts,tsx,js,jsx}

Test Reporters:

  • verbose - Detailed test output with full test names and results

Coverage Reporters:

  • text - Text summary in terminal
  • json - JSON format for CI/CD integration
  • html - HTML coverage report (generated in coverage/ directory)

Use this configuration when you want:

  • ✅ Consistent testing setup across projects
  • ✅ TypeScript support out of the box
  • ✅ Coverage thresholds enforced
  • ✅ Optimized test execution with thread pool
  • ✅ Sensible defaults for Node.js projects
  • ✅ Easy customization and extension

You can extend or override the configuration for your specific needs:

import { defineConfig } from 'vitest/config';
import baseConfig from '@jmlweb/vitest-config';
export default defineConfig({
...baseConfig,
test: {
...baseConfig.test,
// Add custom setup files
setupFiles: ['./src/test/setup.ts'],
// Custom test patterns
include: ['**/*.test.ts', '**/__tests__/**/*.ts'],
// Custom environment
environment: 'jsdom',
},
});

Add test scripts to your package.json:

{
"scripts": {
"test": "vitest",
"test:run": "vitest run",
"test:coverage": "vitest run --coverage",
"test:ui": "vitest --ui",
"test:typecheck": "vitest typecheck"
}
}

Then run:

Terminal window
pnpm test # Run tests in watch mode
pnpm test:run # Run tests once
pnpm test:coverage # Run tests with coverage
pnpm test:ui # Open Vitest UI
pnpm test:typecheck # Run TypeScript type checking

Type checking is performed separately using the vitest typecheck command for better performance. This allows you to run type checking independently of your test suite:

Terminal window
# Run type checking only
pnpm test:typecheck
# Or run tests and type checking together
pnpm test && pnpm test:typecheck
  • Node.js >= 18.0.0
  • Vitest >= 1.0.0
  • TypeScript project (recommended, but not required)

This package requires the following peer dependency:

  • vitest (^1.0.0)

See real-world usage examples:

> Note: This section documents known issues and their solutions. If you encounter a problem not listed here, please open an issue.

Symptoms:

  • Error: “Cannot find module” when running tests
  • Import paths work in the app but fail in tests

Cause:

  • Vitest uses Vite’s module resolution which may differ from TypeScript
  • Path aliases or special imports not configured for tests

Solution:

If you’re using path aliases in tsconfig.json, configure them in your Vitest config:

vitest.config.ts
import { defineConfig } from 'vitest/config';
import vitestConfig from '@jmlweb/vitest-config';
export default defineConfig({
...vitestConfig,
resolve: {
alias: {
'@': '/src',
'@components': '/src/components',
},
},
});

Symptoms:

  • Error: “Environment ‘jsdom’ not found” or “Environment ‘happy-dom’ not found”
  • Tests fail to run with DOM-related errors

Cause:

  • The test environment package is not installed
  • This config specifies the environment but doesn’t install it (peer dependency)

Solution:

Install the DOM environment package:

Terminal window
# For jsdom (more compatible, slower)
pnpm add -D jsdom
# Or for happy-dom (faster, less compatible)
pnpm add -D happy-dom

Then specify in your config if different from default:

vitest.config.ts
import { defineConfig } from 'vitest/config';
import vitestConfig from '@jmlweb/vitest-config';
export default defineConfig({
...vitestConfig,
test: {
...vitestConfig.test,
environment: 'jsdom', // or 'happy-dom'
},
});

Symptoms:

  • No coverage reports generated
  • Coverage command runs but shows no output

Cause:

  • Coverage provider not installed
  • Coverage not enabled in configuration

Solution:

Install the coverage provider:

Terminal window
pnpm add -D @vitest/coverage-v8

Run tests with coverage flag:

Terminal window
vitest --coverage

Symptoms:

  • “No test files found” even though test files exist
  • Vitest doesn’t pick up *.test.ts or *.spec.ts files

Cause:

  • Test file pattern mismatch
  • Files in excluded directories

Solution:

This config includes common patterns. If your tests aren’t found, check:

  1. File naming: Use .test.ts, .spec.ts, .test.tsx, or .spec.tsx
  2. Location: Ensure tests aren’t in node_modules or dist
  3. Explicitly include test patterns:
vitest.config.ts
import { defineConfig } from 'vitest/config';
import vitestConfig from '@jmlweb/vitest-config';
export default defineConfig({
...vitestConfig,
test: {
...vitestConfig.test,
include: ['**/*.{test,spec}.{ts,tsx}'],
},
});

> Note: If no breaking changes were introduced in a version, it’s safe to upgrade without additional steps.

No breaking changes have been introduced yet. This package follows semantic versioning. When breaking changes are introduced, detailed migration instructions will be provided here.

For version history, see the Changelog.

Need Help? If you encounter issues during migration, please open an issue.

See CHANGELOG.md for version history and release notes.

MIT