Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/guide/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ Useful to run with [`lint-staged`](https://github.com/okonet/lint-staged) or wit
vitest related /src/index.ts /src/hello-world.js
```

`vitest related` cannot be used with [`--changed`](/config/changed) or `--since-last-run`. To run the tests affected by the files that git reports as changed, use `vitest run --changed`.

::: tip
Don't forget that Vitest runs with enabled watch mode by default. If you are using tools like `lint-staged`, you should also pass `--run` option, so that command can exit normally.

Expand Down
9 changes: 8 additions & 1 deletion packages/vitest/src/node/config/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,14 @@ export function resolveTestConfig(
}
}
// CLI options are merged into the test config
if ((options as CliOptions).sinceLastRun) {
const { sinceLastRun } = options as CliOptions
if (options.related && (resolved.changed || sinceLastRun)) {
const flag = sinceLastRun ? '--since-last-run' : '--changed'
throw new Error(
`The "${flag}" flag cannot be used with "vitest related". Use "vitest run ${flag}" to run the tests affected by the changed files, or "vitest related <files>" to run the tests affected by the given files.`,
)
}
if (sinceLastRun) {
if (resolved.cache === false) {
throw new Error(
'The "--since-last-run" flag requires the "cache" option to store the dependencies of the test files.',
Expand Down
26 changes: 25 additions & 1 deletion test/e2e/test/git-changed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import { execFileSync } from 'node:child_process'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
import { createFile, editFile, resolvePath, runVitest, useFS, useTmpFS } from '../../test-utils'
import {
createFile,
editFile,
resolvePath,
runInlineTests,
runVitest,
useFS,
useTmpFS,
} from '../../test-utils'

const fileName = 'fixtures/git-changed/related/rerun.temp'

Expand Down Expand Up @@ -87,6 +95,22 @@ it('fails when --changed is used outside of a git repository', async () => {
`)
})

it('fails when "vitest related" is used with --changed or --since-last-run', async () => {
const files = { 'basic.test.js': `import { test } from 'vitest'\ntest('basic', () => {})` }

const changed = await runInlineTests(files, { $cliOptions: { related: [], changed: 'HEAD' } })
expect(changed.stderr.split('\n')[0]).toMatchInlineSnapshot(
`"Error: The "--changed" flag cannot be used with "vitest related". Use "vitest run --changed" to run the tests affected by the changed files, or "vitest related <files>" to run the tests affected by the given files."`,
)

const sinceLastRun = await runInlineTests(files, {
$cliOptions: { related: ['src/a.js'], sinceLastRun: true },
})
expect(sinceLastRun.stderr.split('\n')[0]).toMatchInlineSnapshot(
`"Error: The "--since-last-run" flag cannot be used with "vitest related". Use "vitest run --since-last-run" to run the tests affected by the changed files, or "vitest related <files>" to run the tests affected by the given files."`,
)
})

it('finds changed files with names that git quotes', async () => {
const { root, editFile } = useTmpFS({
'src/café/menu.js': 'export const menu = 1',
Expand Down
Loading