···422422- **Default:** `test`
423423424424Overrides Vite mode.
425425+426426+### changed
427427+428428+- **Type**: `boolean | string`
429429+- **Default**: false
430430+431431+Run tests only against changed files. If no value is provided, it will run tests against uncomitted changes (includes staged and unstaged).
432432+433433+To run tests against changes made in last commit, you can use `--changed HEAD~1`. You can also pass commit hash or branch name.
+1
docs/guide/index.md
···118118| `--environment <env>` | Runner environment (default: `node`) |
119119| `--passWithNoTests` | Pass when no tests found |
120120| `--allowOnly` | Allow tests and suites that are marked as `only` (default: false in CI, true otherwise) |
121121+| `--changed [since]` | Run tests that are affected by the changed files (default: false)
121122| `-h, --help` | Display available CLI options |
122123123124## Examples
+1
packages/vitest/src/node/cli.ts
···3333 .option('--environment <env>', 'runner environment (default: node)')
3434 .option('--passWithNoTests', 'pass when no tests found')
3535 .option('--allowOnly', 'Allow tests and suites that are marked as only (default: !process.env.CI)')
3636+ .option('--changed [since]', 'Run tests that are affected by the changed files (default: false)')
3637 .help()
37383839cli
+3
packages/vitest/src/node/config.ts
···134134 if (!resolved.reporters.length)
135135 resolved.reporters.push('default')
136136137137+ if (resolved.changed)
138138+ resolved.passWithNoTests ??= true
139139+137140 return resolved
138141}
+13
packages/vitest/src/node/core.ts
···1515import { StateManager } from './state'
1616import { resolveConfig } from './config'
1717import { printError } from './error'
1818+import { VitestGit } from './git'
18191920const WATCHER_DEBOUNCE = 100
2021const CLOSE_TIMEOUT = 1_000
···163164 }
164165165166 async filterTestsBySource(tests: string[]) {
167167+ if (this.config.changed && !this.config.related) {
168168+ const vitestGit = new VitestGit(this.config.root)
169169+ const related = await vitestGit.findChangedFiles({
170170+ changedSince: this.config.changed,
171171+ })
172172+ if (!related) {
173173+ this.error(c.red('Could not find Git root. Have you initialized git with `git init`?\n'))
174174+ process.exit(1)
175175+ }
176176+ this.config.related = Array.from(new Set(related))
177177+ }
178178+166179 const related = this.config.related
167180 if (!related)
168181 return tests