Sub topic 13
Configuring Vitest with vitest.config
Vitest runs with sensible defaults, so the earlier chapters needed no configuration. As a project grows, you will want to control which files count as tests, which code runs before every test file, and how mocks behave between tests. This chapter shows where those settings live and how Vitest resolves them. It assumes you have installed Vitest, can run npx vitest, and know the mocking APIs from earlier chapters.
Where Vitest looks for configuration
When Vitest starts, it searches the project root for a config file. A vitest.config.* file (.js, .mjs, .ts, and similar) takes priority over a vite.config.* file. If your app already uses Vite, you can put test settings under the test key in vite.config.js and skip the separate file. Use a dedicated vitest.config.js when you want test settings kept apart from the build settings. Command-line flags override values from either file.
To point at a different file, pass --config, for example npx vitest --config ./config/vitest.unit.js.
A practical config file
Import defineConfig from vitest/config so editors can autocomplete and type-check the test options. All test settings go inside the test object.
// vitest.config.js
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
include: ['src/**/*.test.js'],
exclude: ['**/node_modules/**', 'src/legacy/**'],
setupFiles: ['./vitest.setup.js'],
restoreMocks: true,
testTimeout: 10000,
},
})// vitest.setup.js
import { afterEach, vi } from 'vitest'
afterEach(() => {
vi.useRealTimers()
})Each option does the following:
| Option | Effect | Default |
|---|---|---|
| `include` | Glob patterns for test files | Files ending in `.test` or `.spec` with a JS/TS extension |
| `exclude` | Globs removed from `include` | `node_modules` and `.git`, among others |
| `setupFiles` | Files run before each test file, in that file's context | none |
| `restoreMocks` | Calls `vi.restoreAllMocks()` before each test | `false` |
| `testTimeout` | Milliseconds before a test fails as timed out | `5000` |
| `globals` | Exposes `describe`, `it`, `expect` without imports | `false` |
| `environment` | Runtime for tests, such as `node` or `jsdom` | `node` |
With restoreMocks: true, you no longer need the afterEach(() => vi.restoreAllMocks()) hook from the spies chapter. The setup file does the same job for fake timers across the whole suite. The next chapter covers environment, and the coverage chapter covers the coverage key.
Common errors
- Setting
excludereplaces the defaults. If you set it, list**/node_modules/**yourself, or spreadconfigDefaults.excludefromvitest/config:exclude: [...configDefaults.exclude, 'src/legacy/**']. - Options placed at the top level.
include: [...]outside thetestobject is a Vite option, not a Vitest one, and Vitest silently ignores it for test discovery. - Assuming a setup file runs once.
setupFilesruns before every test file. For one-time work such as starting a database, useglobalSetup. - Turning on
globalsin TypeScript without types. Add"vitest/globals"tocompilerOptions.typesintsconfig.json, or the compiler reportsdescribeas undefined. - Editing config while watch mode runs. Vitest restarts when the config changes. If behavior looks stale, confirm which file was loaded; the terminal header prints the config path.
Exercise
Your vitest.config.js contains exclude: ['e2e/**'], and suddenly Vitest runs test files inside node_modules. Why, and how do you fix it while still excluding e2e?
Show answer
Setting exclude replaces the default list instead of adding to it, so node_modules is no longer excluded. Import configDefaults from 'vitest/config' and use exclude: [...configDefaults.exclude, 'e2e/**'].
Exercise
You want every test to start with real timers and restored spies, without adding hooks to each test file. Which two config options achieve this?
Show answer
Set restoreMocks: true to restore spies before each test, and add a file to setupFiles that registers afterEach(() => vi.useRealTimers()).