Split Vitest Unit and Integration Tests Without a Second Runner
Split Vitest unit and integration tests using workspace globs and fork pools without adding a second test runner.
09 Aug 2025, 10:21 UTC

Your unit tests are waiting on your database
As a repository grows, a single vitest.config.ts often starts pulling in everything: fast pure-function unit tests, component tests, and slower integration tests that hit real databases or external APIs. A one-line change to a utility module can trigger a two-minute test run because Vitest discovers and executes the full test graph under one project. The practical result is slower local feedback and CI pipelines that block on tests unrelated to the change.
The useful takeaway is that Vitest supports multiple workspace projects in a single configuration file. You can define one project for unit tests with a narrow include glob, another for integration tests with a broader glob, and run them independently. You do not need a second test runner or a separate Vitest installation for each suite.
Configure separate projects with include and exclude globs
Vitest workspace projects scope test discovery by file pattern. Glob patterns are relative to the project root, and path separators can differ between Windows and POSIX systems, so forward slashes are the safest choice in configuration files.
import { defineWorkspace } from 'vitest/workspace';
export default defineWorkspace([
{
include: ['src/**/*.unit.test.ts', 'src/**/*.unit.test.tsx'],
exclude: ['src/**/*.integration.test.ts'],
},
{
include: ['src/**/*.integration.test.ts'],
exclude: ['src/**/*.unit.test.ts'],
},
]);
Run these commands from the repository root. No special permissions are required beyond read access to the source tree.
# Run only unit tests
npx vitest run --project unit
# Run only integration tests
npx vitest run --project integration
Expected check: the Vitest output should list only files matching the selected project's include pattern. If files outside the pattern still run, verify that the globs do not overlap and that no parent configuration is re-including them.
Isolate stateful integration tests with the forks pool
Integration tests often mutate shared globals, environment variables, or process state. By default, Vitest runs tests in a shared worker pool, which means one leaky test can affect another. The forks pool runs each test file in its own worker process. Setting singleFork: true isolates every file so state does not bleed between suites.
This isolation costs memory and startup time. Each forked file spawns a separate Node process, so large suites consume more RAM and start more slowly than the default thread pool.
import { defineWorkspace } from 'vitest/workspace';
export default defineWorkspace([
{
include: ['src/**/*.unit.test.ts'],
pool: 'threads',
},
{
include: ['src/**/*.integration.test.ts'],
pool: 'forks',
poolOptions: {
forks: {
singleFork: true,
},
},
},
]);
To verify process isolation, run the integration suite and inspect the worker output. Each test file should appear under its own worker process rather than being batched into shared threads.
npx vitest run --project integration
Risk: if your test files rely on shared in-memory state across files, singleFork: true breaks that assumption by design. Move shared fixtures into setup files or module-scoped mocks instead.
Trade-offs and how to verify the result
Separating projects improves feedback speed, but it introduces configuration to maintain. Changing an include glob after Vitest has cached results can leave stale outcomes until the watcher or cache is cleared. If a test file moves or is renamed, confirm it still appears under the correct project.
Practical verification steps:
- Run
npx vitest run --project unitand confirm no integration test files appear in the output. - Run
npx vitest run --project integrationand confirm only integration-pattern files execute. - Check
npx vitest --versionagainst the Vitest changelog, because workspace and pool behavior can differ between major versions.
Type checking is another consideration. Vitest's typecheck option runs tsc on test files, but it requires TypeScript installed and aligned tsconfig paths if you use path aliases. Enable it only after the project splits are stable.
Closing
Start by splitting your largest test category into its own workspace project. Measure the local run time before and after. If integration tests still feel coupled to unit tests, add the forks pool to the integration project and watch for memory use on CI. The goal is not more configuration; it is faster, more reliable feedback for the tests that actually changed.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.