Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vitest 3.x reference skill covering configuration, test/describe APIs, mocking, coverage, snapshots, and concurrency.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/features-filtering.md
1---2name: test-filtering3description: Filter tests by name, file patterns, and tags4---56# Test Filtering78## CLI Filtering910### By File Path1112```bash13# Run files containing "user"14vitest user1516# Multiple patterns17vitest user auth1819# Specific file20vitest src/user.test.ts2122# By line number23vitest src/user.test.ts:2524```2526### By Test Name2728```bash29# Tests matching pattern30vitest -t "login"31vitest --testNamePattern "should.*work"3233# Regex patterns34vitest -t "/user|auth/"35```3637## Changed Files3839```bash40# Uncommitted changes41vitest --changed4243# Since specific commit44vitest --changed HEAD~145vitest --changed abc1234647# Since branch48vitest --changed origin/main49```5051## Related Files5253Run tests that import specific files:5455```bash56vitest related src/utils.ts src/api.ts --run57```5859Useful with lint-staged:6061```js62// .lintstagedrc.js63export default {64'*.{ts,tsx}': 'vitest related --run',65}66```6768## Focus Tests (.only)6970```ts71test.only('only this runs', () => {})7273describe.only('only this suite', () => {74test('runs', () => {})75})76```7778In CI, `.only` throws error unless configured:7980```ts81defineConfig({82test: {83allowOnly: true, // Allow .only in CI84},85})86```8788## Skip Tests8990```ts91test.skip('skipped', () => {})9293// Conditional94test.skipIf(process.env.CI)('not in CI', () => {})95test.runIf(!process.env.CI)('local only', () => {})9697// Dynamic skip98test('dynamic', ({ skip }) => {99skip(someCondition, 'reason')100})101```102103## Tags104105Tags must be declared in config, then applied to tests/suites and filtered with a tag expression:106107```ts108// vitest.config.ts109defineConfig({110test: {111tags: [{ name: 'db' }, { name: 'slow' }, { name: 'flaky' }],112},113})114115// test file116test('database test', { tags: ['db'] }, () => {})117```118119```bash120vitest --tagsFilter "db && !flaky"121vitest --tagsFilter "unit || e2e"122vitest --list-tags # show defined tags123```124125Full syntax, priority, and per-tag options: see [features-test-tags](features-test-tags.md).126127## Include/Exclude Patterns128129```ts130defineConfig({131test: {132// Test file patterns133include: ['**/*.{test,spec}.{ts,tsx}'],134135// Exclude patterns136exclude: [137'**/node_modules/**',138'**/e2e/**',139'**/*.skip.test.ts',140],141142// Include source for in-source testing143includeSource: ['src/**/*.ts'],144145// Scope discovery to a directory (faster than broad excludes)146dir: './src',147},148})149```150151> v4 simplified default `exclude` to only `node_modules`/`.git`. Prefer `test.dir` to limit where tests are found; spread `configDefaults.exclude` to restore the old excludes.152153## Watch Mode Filtering154155In watch mode, press:156- `p` - Filter by filename pattern157- `t` - Filter by test name pattern158- `a` - Run all tests159- `f` - Run only failed tests160161## Projects Filtering162163Run specific project:164165```bash166vitest --project unit167vitest --project integration --project e2e168```169170## Environment-based Filtering171172```ts173const isDev = process.env.NODE_ENV === 'development'174const isCI = process.env.CI175176describe.skipIf(isCI)('local only tests', () => {})177describe.runIf(isDev)('dev tests', () => {})178```179180## Combining Filters181182```bash183# File pattern + test name + changed184vitest user -t "login" --changed185186# Related files + run mode187vitest related src/auth.ts --run188```189190## List Tests Without Running191192```bash193vitest list # Show all test names194vitest list -t "user" # Filter by name195vitest list --filesOnly # Show only file paths196vitest list --json # JSON output197```198199## Key Points200201- Use `-t` for test name pattern filtering202- `--changed` runs only tests affected by changes203- `--related` runs tests importing specific files204- Tags provide semantic test grouping205- Use `.only` for debugging, but configure CI to reject it206- Watch mode has interactive filtering207208<!--209Source references:210- https://vitest.dev/guide/filtering.html211- https://vitest.dev/guide/cli.html212-->213