Skip to content

Commit 657e83f

Browse files
authored
fix(cli): vitest run --watch should be watch-mode (#8128)
1 parent 04f7a7f commit 657e83f

File tree

6 files changed

+41
-4
lines changed

6 files changed

+41
-4
lines changed

packages/vitest/src/node/cli/cac.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export function parseCLI(argv: string | string[], config: CliParseOptions = {}):
239239
if (arrayArgs[2] === 'watch' || arrayArgs[2] === 'dev') {
240240
options.watch = true
241241
}
242-
if (arrayArgs[2] === 'run') {
242+
if (arrayArgs[2] === 'run' && !options.watch) {
243243
options.run = true
244244
}
245245
if (arrayArgs[2] === 'related') {
@@ -265,7 +265,9 @@ async function watch(cliFilters: string[], options: CliOptions): Promise<void> {
265265
}
266266

267267
async function run(cliFilters: string[], options: CliOptions): Promise<void> {
268-
options.run = true
268+
// "vitest run --watch" should still be watch mode
269+
options.run = !options.watch
270+
269271
await start('test', cliFilters, options)
270272
}
271273

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from "vitest";
2+
3+
test("example", () => {})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default {}

test/config/test/mode.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,25 @@ test.each([
2121
expect(stderr).toContain(`Error: env.mode should be equal to "${expectedMode}"`)
2222
expect(stdout).toBe('')
2323
})
24+
25+
test.each([
26+
{ options: ['run'], expected: 'run' },
27+
{ options: ['run', '--watch'], expected: 'watch' },
28+
{ options: ['watch'], expected: 'watch' },
29+
] as const)(`vitest $options.0 $options.1 resolves to $expected-mode`, async ({ options, expected }) => {
30+
const { vitest } = await testUtils.runVitestCli(...options, '--root', 'fixtures/run-mode')
31+
32+
if (expected === 'watch') {
33+
await vitest.waitForStdout('Test Files 1 passed (1)')
34+
35+
expect(vitest.stdout).not.toContain('RUN')
36+
expect(vitest.stdout).toContain('DEV')
37+
expect(vitest.stdout).toContain('Waiting for file changes')
38+
}
39+
40+
if (expected === 'run') {
41+
expect(vitest.stdout).toContain('RUN')
42+
expect(vitest.stdout).not.toContain('DEV')
43+
expect(vitest.stdout).not.toContain('Waiting for file changes')
44+
}
45+
})

test/core/test/cli-test.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,15 @@ test('public parseCLI works correctly', () => {
381381
'color': true,
382382
},
383383
})
384+
expect(parseCLI('vitest run --watch')).toEqual({
385+
filter: [],
386+
options: {
387+
'watch': true,
388+
'w': true,
389+
'--': [],
390+
'color': true,
391+
},
392+
})
384393
expect(parseCLI('vitest related ./some-files.js')).toEqual({
385394
filter: [],
386395
options: {

test/test-utils/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ async function runCli(command: 'vitest' | 'vite-node', _options?: CliOptions | s
190190
}
191191

192192
// Manually stop the processes so that each test don't have to do this themselves
193-
afterEach(async () => {
193+
onTestFinished(async () => {
194194
if (subprocess.exitCode === null) {
195195
subprocess.kill()
196196
}
@@ -206,7 +206,7 @@ async function runCli(command: 'vitest' | 'vite-node', _options?: CliOptions | s
206206
return output()
207207
}
208208

209-
if (args[0] !== 'list' && args.includes('--watch')) {
209+
if (args[0] !== 'list' && (args.includes('--watch') || args[0] === 'watch')) {
210210
if (command === 'vitest') {
211211
// Wait for initial test run to complete
212212
await cli.waitForStdout('Waiting for file changes')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy