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## Tags104105Filter by custom tags:106107```ts108test('database test', { tags: ['db'] }, () => {})109test('slow test', { tags: ['slow', 'integration'] }, () => {})110```111112Run tagged tests:113114```bash115vitest --tags db116vitest --tags "db,slow" # OR117vitest --tags db --tags slow # OR118```119120Configure allowed tags:121122```ts123defineConfig({124test: {125tags: ['db', 'slow', 'integration'],126strictTags: true, // Fail on unknown tags127},128})129```130131## Include/Exclude Patterns132133```ts134defineConfig({135test: {136// Test file patterns137include: ['**/*.{test,spec}.{ts,tsx}'],138139// Exclude patterns140exclude: [141'**/node_modules/**',142'**/e2e/**',143'**/*.skip.test.ts',144],145146// Include source for in-source testing147includeSource: ['src/**/*.ts'],148},149})150```151152## Watch Mode Filtering153154In watch mode, press:155- `p` - Filter by filename pattern156- `t` - Filter by test name pattern157- `a` - Run all tests158- `f` - Run only failed tests159160## Projects Filtering161162Run specific project:163164```bash165vitest --project unit166vitest --project integration --project e2e167```168169## Environment-based Filtering170171```ts172const isDev = process.env.NODE_ENV === 'development'173const isCI = process.env.CI174175describe.skipIf(isCI)('local only tests', () => {})176describe.runIf(isDev)('dev tests', () => {})177```178179## Combining Filters180181```bash182# File pattern + test name + changed183vitest user -t "login" --changed184185# Related files + run mode186vitest related src/auth.ts --run187```188189## List Tests Without Running190191```bash192vitest list # Show all test names193vitest list -t "user" # Filter by name194vitest list --filesOnly # Show only file paths195vitest list --json # JSON output196```197198## Key Points199200- Use `-t` for test name pattern filtering201- `--changed` runs only tests affected by changes202- `--related` runs tests importing specific files203- Tags provide semantic test grouping204- Use `.only` for debugging, but configure CI to reject it205- Watch mode has interactive filtering206207<!--208Source references:209- https://vitest.dev/guide/filtering.html210- https://vitest.dev/guide/cli.html211-->212