Skip to content

Commit e71a5d0

Browse files
fix: add --changed flag support to vitest list command (fix #8270) (#8272)
1 parent 1638b44 commit e71a5d0

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

packages/vitest/src/node/cli/cli-config.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -886,15 +886,17 @@ export const benchCliOptionsConfig: Pick<
886886
},
887887
}
888888

889-
export const collectCliOptionsConfig: Pick<
890-
VitestCLIOptions,
891-
'json' | 'filesOnly'
892-
> = {
889+
export const collectCliOptionsConfig: VitestCLIOptions = {
890+
...cliOptionsConfig,
893891
json: {
894892
description: 'Print collected tests as JSON or write to a file (Default: false)',
895893
argument: '[true/path]',
896894
},
897895
filesOnly: {
898896
description: 'Print only test files with out the test cases',
899897
},
898+
changed: {
899+
description: 'Print only tests that are affected by the changed files (default: `false`)',
900+
argument: '[since]',
901+
},
900902
}

test/cli/test/list-changed.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { expect, test } from 'vitest'
2+
import { editFile, resolvePath, runVitestCli } from '../../test-utils'
3+
4+
test('list command with --changed flag shows only changed tests', async () => {
5+
const sourceFile = resolvePath(import.meta.url, '../fixtures/git-changed/related/src/sourceA.ts')
6+
7+
// First, run list without --changed to see all tests
8+
const { stdout: allTests } = await runVitestCli('list', '-r=./fixtures/git-changed/related')
9+
expect(allTests).toContain('related.test.ts')
10+
expect(allTests).toContain('not-related.test.ts')
11+
12+
// Now modify the source file to trigger --changed behavior
13+
editFile(sourceFile, content => `${content}\n// modified for test`)
14+
15+
// Run list with --changed flag
16+
const { stdout: changedTests } = await runVitestCli('list', '-r=./fixtures/git-changed/related', '--changed')
17+
expect(changedTests).toContain('related.test.ts')
18+
expect(changedTests).not.toContain('not-related.test.ts')
19+
})
20+
21+
test('list command with --changed flag and --filesOnly shows only changed test files', async () => {
22+
const sourceFile = resolvePath(import.meta.url, '../fixtures/git-changed/related/src/sourceA.ts')
23+
editFile(sourceFile, content => `${content}\n// modified for filesOnly test`)
24+
25+
const { stdout: changedFiles } = await runVitestCli('list', '-r=./fixtures/git-changed/related', '--changed', '--filesOnly')
26+
expect(changedFiles).toContain('related.test.ts')
27+
expect(changedFiles).not.toContain('not-related.test.ts')
28+
})
29+
30+
test('list command with --changed flag and --json outputs changed tests in JSON format', async () => {
31+
const sourceFile = resolvePath(import.meta.url, '../fixtures/git-changed/related/src/sourceA.ts')
32+
editFile(sourceFile, content => `${content}\n// modified for json test`)
33+
34+
const { stdout: changedJson } = await runVitestCli('list', '-r=./fixtures/git-changed/related', '--changed', '--json')
35+
const jsonOutput = JSON.parse(changedJson)
36+
expect(Array.isArray(jsonOutput)).toBe(true)
37+
const relatedTest = jsonOutput.find((test: any) => test.file?.includes('related.test.ts'))
38+
expect(relatedTest).toBeDefined()
39+
})
40+
41+
test('list command with --changed flag when no changes exist should show no tests', async () => {
42+
const { stdout: noChangesOutput } = await runVitestCli('list', '-r=./fixtures/git-changed/related', '--changed')
43+
expect(noChangesOutput.trim()).toBe('')
44+
})

0 commit comments

Comments
 (0)