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
6 changes: 4 additions & 2 deletions packages/vitest/src/node/cli/cac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export function parseCLI(argv: string | string[], config: CliParseOptions = {}):
if (arrayArgs[2] === 'watch' || arrayArgs[2] === 'dev') {
options.watch = true
}
if (arrayArgs[2] === 'run') {
if (arrayArgs[2] === 'run' && !options.watch) {
options.run = true
}
if (arrayArgs[2] === 'related') {
Expand All @@ -265,7 +265,9 @@ async function watch(cliFilters: string[], options: CliOptions): Promise<void> {
}

async function run(cliFilters: string[], options: CliOptions): Promise<void> {
options.run = true
// "vitest run --watch" should still be watch mode
options.run = !options.watch

await start('test', cliFilters, options)
}

Expand Down
3 changes: 3 additions & 0 deletions test/config/fixtures/run-mode/example.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from "vitest";

test("example", () => {})
1 change: 1 addition & 0 deletions test/config/fixtures/run-mode/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {}
22 changes: 22 additions & 0 deletions test/config/test/mode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,25 @@ test.each([
expect(stderr).toContain(`Error: env.mode should be equal to "${expectedMode}"`)
expect(stdout).toBe('')
})

test.each([
{ options: ['run'], expected: 'run' },
{ options: ['run', '--watch'], expected: 'watch' },
{ options: ['watch'], expected: 'watch' },
] as const)(`vitest $options.0 $options.1 resolves to $expected-mode`, async ({ options, expected }) => {
const { vitest } = await testUtils.runVitestCli(...options, '--root', 'fixtures/run-mode')

if (expected === 'watch') {
await vitest.waitForStdout('Test Files 1 passed (1)')

expect(vitest.stdout).not.toContain('RUN')
expect(vitest.stdout).toContain('DEV')
expect(vitest.stdout).toContain('Waiting for file changes')
}

if (expected === 'run') {
expect(vitest.stdout).toContain('RUN')
expect(vitest.stdout).not.toContain('DEV')
expect(vitest.stdout).not.toContain('Waiting for file changes')
}
})
9 changes: 9 additions & 0 deletions test/core/test/cli-test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,15 @@ test('public parseCLI works correctly', () => {
'color': true,
},
})
expect(parseCLI('vitest run --watch')).toEqual({
filter: [],
options: {
'watch': true,
'w': true,
'--': [],
'color': true,
},
})
expect(parseCLI('vitest related ./some-files.js')).toEqual({
filter: [],
options: {
Expand Down
4 changes: 2 additions & 2 deletions test/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ async function runCli(command: 'vitest' | 'vite-node', _options?: CliOptions | s
}

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

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