Skip to content

Commit 013bf2c

Browse files
authored
fix!: ignore --standalone when CLI filename filter is used (#8262)
1 parent 88071a8 commit 013bf2c

File tree

9 files changed

+78
-6
lines changed

9 files changed

+78
-6
lines changed

docs/guide/cli-generated.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ Enables coverage collection. Can be overridden using the `--coverage` CLI option
117117
- **CLI:** `--coverage.include <pattern>`
118118
- **Config:** [coverage.include](/config/#coverage-include)
119119

120-
Files included in coverage as glob patterns. May be specified more than once when using multiple patterns (default: `**`)
120+
Files included in coverage as glob patterns. May be specified more than once when using multiple patterns. By default only files covered by tests are included.
121121

122122
### coverage.exclude
123123

124124
- **CLI:** `--coverage.exclude <pattern>`
125125
- **Config:** [coverage.exclude](/config/#coverage-exclude)
126126

127-
Files to be excluded in coverage. May be specified more than once when using multiple extensions (default: Visit [`coverage.exclude`](https://vitest.dev/config/#coverage-exclude))
127+
Files to be excluded in coverage. May be specified more than once when using multiple extensions.
128128

129129
### coverage.clean
130130

@@ -929,4 +929,4 @@ Use `bundle` to bundle the config with esbuild or `runner` (experimental) to pro
929929

930930
- **CLI:** `--standalone`
931931

932-
Start Vitest without running tests. File filters will be ignored, tests will be running only on change (default: `false`)
932+
Start Vitest without running tests. Tests will be running only on change. This option is ignored when CLI file filters are passed. (default: `false`)

docs/guide/migration.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,35 @@ const mock = new Spy()
116116

117117
Note that now if you provide an arrow function, you will get [`<anonymous> is not a constructor` error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_constructor) when the mock is called.
118118

119+
### Standalone mode with filename filter
120+
121+
To improve user experience, Vitest will now start running the matched files when [`--standalone`](/guide/cli#standalone) is used with filename filter.
122+
123+
```sh
124+
# In Vitest v3 and below this command would ignore "math.test.ts" filename filter.
125+
# In Vitest v4 the math.test.ts will run automatically.
126+
$ vitest --standalone math.test.ts
127+
```
128+
129+
This allows users to create re-usable `package.json` scripts for standalone mode.
130+
131+
::: code-group
132+
```json [package.json]
133+
{
134+
"scripts": {
135+
"test:dev": "vitest --standalone"
136+
}
137+
}
138+
```
139+
```bash [CLI]
140+
# Start Vitest in standalone mode, without running any files on start
141+
$ pnpm run test:dev
142+
143+
# Run math.test.ts immediately
144+
$ pnpm run test:dev math.test.ts
145+
```
146+
:::
147+
119148
### Deprecated APIs are Removed
120149

121150
Vitest 4.0 removes some deprecated APIs, including:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ async function collect(mode: VitestRunMode, cliFilters: string[], options: CliOp
341341
...normalizeCliOptions(cliFilters, options),
342342
watch: false,
343343
run: true,
344-
})
344+
}, undefined, undefined, cliFilters)
345345
if (!options.filesOnly) {
346346
const { testModules: tests, unhandledErrors: errors } = await ctx.collect(cliFilters.map(normalize))
347347

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export async function startVitest(
5757
options,
5858
viteOverrides,
5959
vitestOptions,
60+
cliFilters,
6061
)
6162

6263
if (mode === 'test' && ctx.config.coverage.enabled) {
@@ -139,6 +140,7 @@ export async function prepareVitest(
139140
options: CliOptions = {},
140141
viteOverrides?: ViteUserConfig,
141142
vitestOptions?: VitestOptions,
143+
cliFilters?: string[],
142144
): Promise<Vitest> {
143145
process.env.TEST = 'true'
144146
process.env.VITEST = 'true'
@@ -148,6 +150,10 @@ export async function prepareVitest(
148150
options.watch = false
149151
}
150152

153+
if (options.standalone && (cliFilters?.length || 0) > 0) {
154+
options.standalone = false
155+
}
156+
151157
// this shouldn't affect _application root_ that can be changed inside config
152158
const root = resolve(options.root || process.cwd())
153159

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ export const cliOptionsConfig: VitestCLIOptions = {
820820
},
821821
standalone: {
822822
description:
823-
'Start Vitest without running tests. File filters will be ignored, tests will be running only on change (default: `false`)',
823+
'Start Vitest without running tests. Tests will be running only on change. This option is ignored when CLI file filters are passed. (default: `false`)',
824824
},
825825
mergeReports: {
826826
description:

packages/vitest/src/node/types/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ export interface UserConfig extends InlineConfig {
912912
*
913913
* Vitest will only run tests if it's called programmatically or the test file changes.
914914
*
915-
* CLI file filters will be ignored.
915+
* If CLI file filters are passed, standalone mode is ignored.
916916
*/
917917
standalone?: boolean
918918

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expect, test } from "vitest";
2+
3+
test("example", () => {
4+
expect(1).toBe(1);
5+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({})

test/cli/test/standalone.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { test } from 'vitest'
2+
import { runVitest } from '../../test-utils'
3+
4+
test('test run is not started when --standalone', async () => {
5+
const { vitest } = await runVitest({
6+
root: 'fixtures/standalone',
7+
standalone: true,
8+
watch: true,
9+
})
10+
11+
await vitest.waitForStdout('Vitest is running in standalone mode. Edit a test file to rerun tests.')
12+
await vitest.waitForStdout('PASS Waiting for file changes...')
13+
await vitest.waitForStdout('press h to show help, press q to quit')
14+
})
15+
16+
test('test run is started when --standalone and filename filter', async () => {
17+
const { vitest } = await runVitest({
18+
root: 'fixtures/standalone',
19+
standalone: true,
20+
watch: true,
21+
}, ['basic.test.ts'])
22+
23+
await vitest.waitForStdout('✓ basic.test.ts > example')
24+
await vitest.waitForStdout('Test Files 1 passed (1)')
25+
await vitest.waitForStdout('Tests 1 passed (1)')
26+
27+
await vitest.waitForStdout('PASS Waiting for file changes...')
28+
await vitest.waitForStdout('press h to show help, press q to quit')
29+
})

0 commit comments

Comments
 (0)