From 0ae54ea8086543dfbf59c7cc0500a7deb9138768 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Thu, 19 Nov 2026 12:16:31 +0100 Subject: [PATCH] fix(cli): disallow --changed in vitest related --- docs/guide/cli.md | 2 ++ .../vitest/src/node/config/resolveConfig.ts | 9 ++++++- test/e2e/test/git-changed.test.ts | 26 ++++++++++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/docs/guide/cli.md b/docs/guide/cli.md index f9b80c99718b..cfb3a6b8746c 100644 --- a/docs/guide/cli.md +++ b/docs/guide/cli.md @@ -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. diff --git a/packages/vitest/src/node/config/resolveConfig.ts b/packages/vitest/src/node/config/resolveConfig.ts index bde1acc5309f..21089d782f21 100644 --- a/packages/vitest/src/node/config/resolveConfig.ts +++ b/packages/vitest/src/node/config/resolveConfig.ts @@ -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 " 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.', diff --git a/test/e2e/test/git-changed.test.ts b/test/e2e/test/git-changed.test.ts index 93a4116aa14a..7e5a95c07b86 100644 --- a/test/e2e/test/git-changed.test.ts +++ b/test/e2e/test/git-changed.test.ts @@ -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' @@ -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 " 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 " 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',