— Tooling · @vitus-labs/tools-vitest

One test config for every package.

createVitestConfig() returns a complete Vitest config: globals on, mocks reset between tests, tests found in src/, V8 coverage held to 90%. Pass the few options that differ, like a DOM environment, aliases or setup files, and every package tests the same way.

@vitus-labs/tools-vitestv2.9.1 · MIT
npm i -D @vitus-labs/tools-vitest vitest vite
function call per package
1
default coverage thresholds
90%
options, all optional
13
peer range
vitest ≥ 4

— Why tools · vitest

Stop copying vitest.config.ts around.

01

Same defaults everywhere

Globals, mockReset, the node environment, src/**/*.test.ts(x) discovery and a lib/** exclude are set once. A package states only how it differs.

02

Coverage with a floor

The v8 provider over src/**/*.ts(x), minus tests, every index.ts (not only barrels) and src/bin/**, with 90% thresholds for statements, branches, functions and lines. Lower any one of them per package.

03

Aliases that survive the monorepo root

Aliases are resolved by a small plugin once the project root is known, so ~/ still points at the right src/ when Vitest runs every package from the root via test.projects.

04

Extend, never fork

The result is a regular defineConfig object, and the default coverage globs are exported. Spread it, mergeConfig it, or override a field; the UI system's shared config does exactly that.

— Before / after

Same setup, seven lines.

A React package tested in jsdom with jest-dom matchers, a ~/ alias and coverage gates. By hand, every package carries its own copy of the whole block.

beforeHand-rolled Vitest config
vitest.config.ts
import { resolve } from 'node:path'
import { configDefaults, defineConfig } from 'vitest/config'

export default defineConfig({
  resolve: {
    alias: { '~': resolve(import.meta.dirname, 'src') },
  },
  test: {
    globals: true,
    environment: 'jsdom',
    mockReset: true,
    setupFiles: ['@testing-library/jest-dom/vitest'],
    include: ['src/**/*.test.ts', 'src/**/*.test.tsx'],
    exclude: [...configDefaults.exclude, 'lib/**'],
    css: { modules: { classNameStrategy: 'non-scoped' } },
    coverage: {
      provider: 'v8',
      include: ['src/**/*.ts', 'src/**/*.tsx'],
      exclude: ['src/**/*.test.ts', 'src/**/*.test.tsx', 'src/**/index.ts', 'src/bin/**'],
      thresholds: { statements: 90, branches: 90, functions: 90, lines: 90 },
    },
  },
})

Copied into each package. Every copy drifts on its own: a threshold here, an exclude there.

after@vitus-labs/tools-vitest
vitest.config.ts
import { createVitestConfig } from '@vitus-labs/tools-vitest'

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

Everything else is the shared default. Change it in one place and every package follows.

— Features

Thirteen knobs, sane defaults.

  • environment

    node by default; jsdom, happy-dom or any Vitest environment

  • aliases · root

    relative targets resolve against the project root, or a root you pass

  • setupFiles

    matchers and globals loaded before each test file

  • plugins

    Vite plugins appended after the alias plugin

  • css

    off by default: CSS imports are empty and CSS module class names unscoped

  • include · exclude

    extra test globs, appended to the src/ defaults

  • coverageInclude · coverageExclude

    extra coverage globs on top of the defaults

  • coverageThresholds

    override one metric and keep the others at 90

  • testTimeout · pool

    passed straight through; Vitest defaults when omitted

  • string[] shorthand

    createVitestConfig([...]) is the same as coverageExclude

  • DEFAULT_COVERAGE_*

    the default include and exclude globs, exported for mergeConfig setups

— API

One function. Four exports beside it.

Every name links to its reference in the docs.

— Honest limits

When the defaults get in the way.

Where it isn't the right fit, and what to reach for instead.

  • Tests that live outside src/

    Discovery always includes src/**/*.test.ts(x); include adds globs but cannot remove those. For a tests/ folder, add it via include. To replace discovery entirely, override test.include on the returned config.

  • A new package below 90%

    Thresholds default to 90% on all four metrics, so a young package fails --coverage until it gets there. Set coverageThresholds to where it is today and raise it over time.

  • Coverage settings in a monorepo

    When Vitest runs from the monorepo root, coverage options are only read from the root config, not from each package. Put shared coverage settings in the root vitest.config.ts.

  • You are not on Vitest

    It is a Vitest config factory with vitest >= 4 and Vite 6 to 8 as peers. A Jest setup gains nothing from it; keep your Jest config or migrate first.

— Recipes

Copy, paste, ship.

01

React components in jsdom

jest-dom matchers, a ~/ alias and real CSS processing for a package whose tests assert on computed styles.

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

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

Run every package from the root

The root lists packages as projects (vitest.workspace.ts is gone since Vitest 4); each package keeps its one-line config.

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

export default defineConfig({
  test: {
    projects: ['packages/*'],
  },
})

// packages/*/vitest.config.ts
// export default createVitestConfig({ aliases: { '~/': 'src/' } })
03

Wrap it in a team preset

The pattern the UI system uses: call the factory once in a shared file, then add what Vitest's own config needs, here resolve.conditions and a project name.

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

export default ({ name }: { name: string }) => {
  const config = createVitestConfig({
    environment: 'jsdom',
    setupFiles: ['../../vitest.setup.ts'],
  })

  return {
    ...config,
    resolve: { ...config.resolve, conditions: ['source'] },
    test: { ...config.test, name },
  }
}

— FAQ

Questions, answered.

Does it work with Vitest 5?

Yes. The peer range is vitest >= 4, and the tools repo itself tests on Vitest 5.

Why are my CSS module class names unscoped?

With css off (the default), CSS files load as empty strings and CSS modules use non-scoped class names, so tests can assert on stable names. Pass css: true to process CSS for real.

Do I need @vitest/coverage-v8?

Only to run with --coverage. It is an optional peer; the preset always configures the v8 provider.

Who uses it?

The Vitus Labs UI system's shared test config is built on createVitestConfig, across every package.

— @vitus-labs/tools-vitest

Write the test config once.

npm i -D @vitus-labs/tools-vitest vitest vite
Sources · 21 claims, each traced to v2.9.1 code or docs
  1. createVitestConfig sets globals, mockReset, node environment by defaulttools:packages/vitest/src/index.ts
  2. Mock reset enabledtools:packages/vitest/src/index.ts
  3. Tests discovered in src/**/*.test.ts(x); include appendstools:packages/vitest/src/index.ts
  4. lib/** excluded from discoverytools:packages/vitest/src/index.ts
  5. Coverage via v8 providertools:packages/vitest/src/index.ts
  6. 90% thresholds for statements, branches, functions, lines; per-metric overridetools:packages/vitest/src/index.ts
  7. Default coverage include src/**/*.ts(x)tools:packages/vitest/src/index.ts
  8. Default coverage exclude: tests, index.ts, src/bin/**tools:packages/vitest/src/index.ts
  9. Aliases resolved lazily once the project root is known (works from monorepo root)tools:packages/vitest/src/index.ts
  10. 13 options in VitestConfigOptions, all optionaltools:packages/vitest/src/index.ts
  11. css off: CSS files empty strings, CSS modules non-scopedtools:packages/vitest/src/index.ts
  12. String array shorthand = coverageExcludetools:packages/vitest/src/index.ts
  13. Plugins appended after the alias plugintools:packages/vitest/src/index.ts
  14. Returns a regular defineConfig result; defaults exported for mergeConfigtools:packages/vitest/src/index.ts
  15. Peers: vitest >= 4, vite ^6 || ^7 || ^8, optional @vitest/coverage-v8tools:packages/vitest/package.json
  16. Coverage peer is optionaltools:packages/vitest/package.json
  17. The tools repo tests on Vitest 5tools:packages/vitest/package.json
  18. Coverage options only read from the root config in a monorepotools:packages/vitest/README.md
  19. vitest.workspace.ts / defineWorkspace removed in Vitest 4; use test.projectstools:packages/vitest/README.md
  20. UI system's shared test config is built on createVitestConfigui-system:vitest.shared.ts
  21. UI system wrapper adds resolve.conditions and a project nameui-system:vitest.shared.ts