Setting the file. One moment.
Subchapter 13.15
references/features-filtering.mdMarkdown4 KBView on GitHub
# Run files containing "user"
vitest user
# Multiple patterns
vitest user auth
# Specific file
vitest src/user.test.ts
# By line number
vitest src/user.test.ts:25# Tests matching pattern
vitest -t "login"
vitest --testNamePattern "should.*work"
# Regex patterns
vitest -t "/user|auth/"# Uncommitted changes
vitest --changed
# Since specific commit
vitest --changed HEAD~1
vitest --changed abc123
# Since branch
vitest --changed origin/mainRun tests that import specific files:
vitest related src/utils.ts src/api.ts --runUseful with lint-staged:
// .lintstagedrc.js
export default {
'*.{ts,tsx}': 'vitest related --run',
}test.only('only this runs', () => {})
describe.only('only this suite', () => {
test('runs', () => {})
})In CI, .only throws error unless configured:
defineConfig({
test: {
allowOnly: true, // Allow .only in CI
},
})test.skip('skipped', () => {})
// Conditional
test.skipIf(process.env.CI)('not in CI', () => {})
test.runIf(!process.env.CI)('local only', () => {})
// Dynamic skip
test('dynamic', ({ skip }) => {
skip(someCondition, 'reason')
})Tags must be declared in config, then applied to tests/suites and filtered with a tag expression:
// vitest.config.ts
defineConfig({
test: {
tags: [{ name: 'db' }, { name: 'slow' }, { name: 'flaky' }],
},
})
// test file
test('database test', { tags: ['db'] }, () => {})vitest --tagsFilter "db && !flaky"
vitest --tagsFilter "unit || e2e"
vitest --list-tags # show defined tagsFull syntax, priority, and per-tag options: see features-test-tags.
defineConfig({
test: {
// Test file patterns
include: ['**/*.{test,spec}.{ts,tsx}'],
// Exclude patterns
exclude: [
'**/node_modules/**',
'**/e2e/**',
'**/*.skip.test.ts',
],
// Include source for in-source testing
includeSource: ['src/**/*.ts'],
// Scope discovery to a directory (faster than broad excludes)
dir: './src',
},
})v4 simplified default
excludeto onlynode_modules/.git. Prefertest.dirto limit where tests are found; spreadconfigDefaults.excludeto restore the old excludes.
In watch mode, press:
p - Filter by filename patternt - Filter by test name patterna - Run all testsf - Run only failed testsRun specific project:
vitest --project unit
vitest --project integration --project e2econst isDev = process.env.NODE_ENV === 'development'
const isCI = process.env.CI
describe.skipIf(isCI)('local only tests', () => {})
describe.runIf(isDev)('dev tests', () => {})# File pattern + test name + changed
vitest user -t "login" --changed
# Related files + run mode
vitest related src/auth.ts --runvitest list # Show all test names
vitest list -t "user" # Filter by name
vitest list --filesOnly # Show only file paths
vitest list --json # JSON output-t for test name pattern filtering--changed runs only tests affected by changes--related runs tests importing specific files.only for debugging, but configure CI to reject it