Skip to Content
vitus·labs
Tools Vitest

Tools Vitest

Shared Vitest configuration factory with sensible defaults for TypeScript + Vite projects.

@vitus-labs/tools-vitest provides a factory function for generating consistent Vitest configurations across a monorepo. It exports createVitestConfig() with opinionated defaults — globals enabled, V8 coverage at 90%, and automatic test discovery.

Installation

npm install @vitus-labs/tools-vitest

Requirements

  • Node.js >= 22
  • Peer dependencies: vitest >= 4 (Vitest 5 supported), vite ^6 || ^7 || ^8
  • Optional peer: @vitest/coverage-v8 >= 4 — install it to run with --coverage (the preset uses the v8 provider)

Quick Start

Create a vitest.config.ts in your package:

import { createVitestConfig } from '@vitus-labs/tools-vitest'

export default createVitestConfig()

That's it. You get globals, V8 coverage with 90% thresholds, and src/**/*.test.{ts,tsx} discovery.

Configuration Options

createVitestConfig(options?: VitestConfigOptions | string[]): UserConfig

VitestConfigOptions

OptionTypeDefaultDescription
environmentstring'node'Test environment: 'node', 'jsdom', 'happy-dom'
cssbooleanfalseEnable full CSS processing (if false, CSS files become empty strings and CSS modules are non-scoped)
setupFilesstring[]—Files to run before each test
aliasesRecord<string, string>—Path aliases; relative targets are resolved against root
rootstringproject rootDirectory relative aliases targets resolve against (defaults to the Vite/Vitest project root, then process.cwd())
pluginsPluginOption[]—Vite plugins for test bundling
testTimeoutnumber5000 (Vitest default)Test timeout in milliseconds
pool'threads' | 'forks' | 'vmThreads' | 'vmForks''forks' (Vitest default)Worker pool strategy
includestring[]—Extra test discovery patterns (appended to defaults)
excludestring[]—Extra exclusion patterns (appended to defaults)
coverageExcludestring[]—Extra coverage exclusion patterns (appended to defaults)
coverageIncludestring[]—Extra coverage inclusion patterns (appended to defaults)
coverageThresholdsCoverageThresholdsAll 90%Override coverage thresholds

CoverageThresholds

interface CoverageThresholds {
  statements?: number   // Default: 90
  branches?: number     // Default: 90
  functions?: number    // Default: 90
  lines?: number        // Default: 90
}

Default Behavior

Test Discovery

include: ['src/**/*.test.ts', 'src/**/*.test.tsx']
exclude: [...vitest defaults, 'lib/**']

Test files must live under src/ and match *.test.ts or *.test.tsx. The lib/ directory is always excluded.

Coverage

provider: 'v8'
include: ['src/**/*.ts', 'src/**/*.tsx']
exclude: ['src/**/*.test.ts', 'src/**/*.test.tsx', 'src/**/index.ts', 'src/bin/**']
  • V8 provider — uses Node's built-in coverage (fast, no instrumentation)
  • Index files excluded — re-export barrels don't count toward coverage
  • Bin scripts excluded — CLI entry points excluded
  • 90% thresholds — statements, branches, functions, and lines

Globals

describe, it, expect, vi, beforeEach, afterEach, etc. are available without imports.

Mock Reset

All mocks are automatically reset between tests (mockReset: true).

Examples

DOM Testing (React Components)

import { createVitestConfig } from '@vitus-labs/tools-vitest'

export default createVitestConfig({
  environment: 'jsdom',
  css: true,
  setupFiles: ['@testing-library/jest-dom/vitest'],
})

Custom Path Aliases

import { createVitestConfig } from '@vitus-labs/tools-vitest'

export default createVitestConfig({
  aliases: { '~/': 'src/' },
})

A trailing / on the key is dropped, and relative targets are resolved lazily against the Vite/Vitest project root (or the root option), so ~/ becomes {root}/src. Absolute targets are kept as-is. Because resolution happens once the project root is known, aliases also work when Vitest runs from a monorepo root via test.projects.

export default createVitestConfig({
  aliases: { '~/': 'src/' },
  root: new URL('.', import.meta.url).pathname, // pin the base explicitly
})

Relaxed Coverage Thresholds

import { createVitestConfig } from '@vitus-labs/tools-vitest'

export default createVitestConfig({
  coverageThresholds: {
    statements: 80,
    branches: 70,
  },
  // functions and lines remain at 90% default
})

With Vite Plugins

import { createVitestConfig } from '@vitus-labs/tools-vitest'
import tsconfigPaths from 'vite-tsconfig-paths'

export default createVitestConfig({
  plugins: [tsconfigPaths()],
})

Legacy Shorthand

A string array is treated as coverageExclude:

// Equivalent to: { coverageExclude: ['src/storybook/**'] }
export default createVitestConfig(['src/storybook/**'])

Monorepo Setup

Each package defines its own vitest.config.ts. A root vitest.config.ts lists them as projects (vitest.workspace.ts / defineWorkspace were removed in Vitest 4):

// vitest.config.ts (root)
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    projects: ['packages/*'],
  },
})
// packages/my-lib/vitest.config.ts
import { createVitestConfig } from '@vitus-labs/tools-vitest'

export default createVitestConfig({
  environment: 'jsdom',
})
// packages/my-app/vitest.config.ts
import { createVitestConfig } from '@vitus-labs/tools-vitest'

export default createVitestConfig({
  environment: 'node',
})

When running from the monorepo root, Vitest only reads coverage options from the root config — per-package coverageThresholds etc. apply when the package is run on its own.

CSS Handling

When css: false (default), imported CSS files are replaced with empty strings and CSS modules resolve to non-scoped class names during testing. This prevents tests from breaking when CSS class names change.

When css: true, full CSS processing is enabled — useful when testing CSS-dependent logic.

Exported Defaults

For consumers using mergeConfig, the default coverage arrays are exported so they can be spread into overrides:

import {
  createVitestConfig,
  DEFAULT_COVERAGE_EXCLUDE,
  DEFAULT_COVERAGE_INCLUDE,
} from '@vitus-labs/tools-vitest'

import { mergeConfig } from 'vitest/config'

// Recommended: use coverageExclude option directly
export default createVitestConfig({
  coverageExclude: ['src/generated/**'],
})

// Alternative: use mergeConfig with exported defaults
export default mergeConfig(createVitestConfig(), {
  test: {
    coverage: {
      exclude: [...DEFAULT_COVERAGE_EXCLUDE, 'src/generated/**'],
    },
  },
})

Default Coverage Patterns

DEFAULT_COVERAGE_INCLUDE = ['src/**/*.ts', 'src/**/*.tsx']
DEFAULT_COVERAGE_EXCLUDE = ['src/**/*.test.ts', 'src/**/*.test.tsx', 'src/**/index.ts', 'src/bin/**']

All Exports

import {
  createVitestConfig,
  DEFAULT_COVERAGE_EXCLUDE,
  DEFAULT_COVERAGE_INCLUDE,
} from '@vitus-labs/tools-vitest'

import type {
  VitestConfigOptions,
  CoverageThresholds,
} from '@vitus-labs/tools-vitest'
Edit on GitHub

Last updated on

On this page