Skip to content
jshacks.dev
Testing

Run Vitest with a different config per folder

Give each folder its own Vitest config with the projects option, so unit and integration tests get different timeouts and setup in one run.

1 min read
Summarize in AI

Fast unit tests and slow database tests want different timeouts, setup files and environments. Instead of two commands, declare two projects that point at their own configs.

vitest.config.jsJavaScript
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    projects: ['tests/unit', 'tests/integration'],
  },
});
tests/integration/vitest.config.jsJavaScript
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    name: 'integration',
    include: ['**/*.test.js'],
    testTimeout: 30_000,
    setupFiles: ['./setup-db.js'],
  },
});
tests/unit/vitest.config.jsJavaScript
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    name: 'unit',
    include: ['**/*.test.js'],
    testTimeout: 2_000,
  },
});
Text
 ✓ |unit| tests/unit/cart.test.js (3 tests)
 ✓ |integration| tests/integration/orders.test.js (2 tests)

Each folder in projects is searched for a config file, and its options apply only to that project. Run one alone with vitest --project integration. This needs Vitest 3.2 or newer, where projects replaced the separate workspace file, per the Vitest projects guide.

Include paths inside a project config are relative to that config's folder, not to the repository root, which is a common source of "no test files found".

Taggedvitest
Share

More snippets

Debugging

Debug a single Vitest test in VS Code

Run one Vitest file under the VS Code debugger with a launch configuration that passes the open file, so breakpoints in tests and source work. Vitest 3.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.