— 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.
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.
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.
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.
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.
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.
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.
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
nodeby default;jsdom,happy-domor any Vitest environment - aliases · root
relative targets resolve against the project root, or a
rootyou 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 ascoverageExclude - DEFAULT_COVERAGE_*
the default include and exclude globs, exported for
mergeConfigsetups
— 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);includeadds globs but cannot remove those. For atests/folder, add it viainclude. To replace discovery entirely, overridetest.includeon the returned config.A new package below 90%
Thresholds default to 90% on all four metrics, so a young package fails
--coverageuntil it gets there. SetcoverageThresholdsto 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 >= 4and Vite 6 to 8 as peers. A Jest setup gains nothing from it; keep your Jest config or migrate first.
— Recipes
Copy, paste, ship.
React components in jsdom
jest-dom matchers, a ~/ alias and real CSS processing for a package whose tests assert on computed styles.
import { createVitestConfig } from '@vitus-labs/tools-vitest'
export default createVitestConfig({
environment: 'jsdom',
setupFiles: ['@testing-library/jest-dom/vitest'],
aliases: { '~/': 'src/' },
css: true,
})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.
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
projects: ['packages/*'],
},
})
// packages/*/vitest.config.ts
// export default createVitestConfig({ aliases: { '~/': 'src/' } })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.
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 viteSources · 21 claims, each traced to v2.9.1 code or docs
- createVitestConfig sets globals, mockReset, node environment by defaulttools:packages/vitest/src/index.ts
- Mock reset enabledtools:packages/vitest/src/index.ts
- Tests discovered in src/**/*.test.ts(x); include appendstools:packages/vitest/src/index.ts
- lib/** excluded from discoverytools:packages/vitest/src/index.ts
- Coverage via v8 providertools:packages/vitest/src/index.ts
- 90% thresholds for statements, branches, functions, lines; per-metric overridetools:packages/vitest/src/index.ts
- Default coverage include src/**/*.ts(x)tools:packages/vitest/src/index.ts
- Default coverage exclude: tests, index.ts, src/bin/**tools:packages/vitest/src/index.ts
- Aliases resolved lazily once the project root is known (works from monorepo root)tools:packages/vitest/src/index.ts
- 13 options in VitestConfigOptions, all optionaltools:packages/vitest/src/index.ts
- css off: CSS files empty strings, CSS modules non-scopedtools:packages/vitest/src/index.ts
- String array shorthand = coverageExcludetools:packages/vitest/src/index.ts
- Plugins appended after the alias plugintools:packages/vitest/src/index.ts
- Returns a regular defineConfig result; defaults exported for mergeConfigtools:packages/vitest/src/index.ts
- Peers: vitest >= 4, vite ^6 || ^7 || ^8, optional @vitest/coverage-v8tools:packages/vitest/package.json
- Coverage peer is optionaltools:packages/vitest/package.json
- The tools repo tests on Vitest 5tools:packages/vitest/package.json
- Coverage options only read from the root config in a monorepotools:packages/vitest/README.md
- vitest.workspace.ts / defineWorkspace removed in Vitest 4; use test.projectstools:packages/vitest/README.md
- UI system's shared test config is built on createVitestConfigui-system:vitest.shared.ts
- UI system wrapper adds resolve.conditions and a project nameui-system:vitest.shared.ts