Skip to content

Commit 2a8520a

Browse files
janis-mehi-ogawa
andauthored
feat: support --configLoader CLI option (#7574)
Co-authored-by: Hiroshi Ogawa <[email protected]>
1 parent 1ef31a7 commit 2a8520a

File tree

18 files changed

+105
-4
lines changed

18 files changed

+105
-4
lines changed

docs/.vitepress/scripts/cli-generator.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const skipConfig = new Set([
3030
'coverage.thresholds.lines',
3131
'standalone',
3232
'clearScreen',
33+
'configLoader',
3334
'color',
3435
'run',
3536
'hideSkippedTests',

docs/advanced/api/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ The ["Running Tests"](/advanced/guide/tests#startvitest) guide has a usage examp
6161
```ts
6262
function createVitest(
6363
mode: VitestRunMode,
64-
options: UserConfig,
64+
options: CliOptions,
6565
viteOverrides: ViteUserConfig = {},
6666
vitestOptions: VitestOptions = {},
6767
): Promise<Vitest>

docs/guide/cli-generated.md

+6
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,12 @@ Removes colors from the console output
919919

920920
Clear terminal screen when re-running tests during watch mode (default: `true`)
921921

922+
### configLoader
923+
924+
- **CLI:** `--configLoader <loader>`
925+
926+
Use `bundle` to bundle the config with esbuild or `runner` (experimental) to process it on the fly. This is only available in vite version 6.1.0 and above. (default: `bundle`)
927+
922928
### standalone
923929

924930
- **CLI:** `--standalone`

packages/browser/src/node/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export async function createBrowserServer(
5858
},
5959
mode: project.config.mode,
6060
configFile: configPath,
61+
configLoader: project.vite.config.inlineConfig.configLoader,
6162
// watch is handled by Vitest
6263
server: {
6364
hmr: false,

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { UserConfig as ViteUserConfig } from 'vite'
1+
import type { InlineConfig as ViteInlineConfig, UserConfig as ViteUserConfig } from 'vite'
22
import type { environments } from '../../integrations/env'
33
import type { Vitest, VitestOptions } from '../core'
44
import type { TestModule, TestSuite } from '../reporters/reported-tasks'
@@ -28,6 +28,14 @@ export interface CliOptions extends UserConfig {
2828
* Output collected test files only
2929
*/
3030
filesOnly?: boolean
31+
32+
/**
33+
* Override vite config's configLoader from cli.
34+
* Use `bundle` to bundle the config with esbuild or `runner` (experimental) to process it on the fly (default: `bundle`).
35+
* This is only available with **vite version 6.1.0** and above.
36+
* @experimental
37+
*/
38+
configLoader?: ViteInlineConfig extends { configLoader?: infer T } ? T : never
3139
}
3240

3341
/**

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

+5
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,11 @@ export const cliOptionsConfig: VitestCLIOptions = {
806806
description:
807807
'Clear terminal screen when re-running tests during watch mode (default: `true`)',
808808
},
809+
configLoader: {
810+
description:
811+
'Use `bundle` to bundle the config with esbuild or `runner` (experimental) to process it on the fly. This is only available in vite version 6.1.0 and above. (default: `bundle`)',
812+
argument: '<loader>',
813+
},
809814
standalone: {
810815
description:
811816
'Start Vitest without running tests. File filters will be ignored, tests will be running only on change (default: `false`)',

packages/vitest/src/node/create.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import type {
22
InlineConfig as ViteInlineConfig,
33
UserConfig as ViteUserConfig,
44
} from 'vite'
5+
import type { CliOptions } from './cli/cli-api'
56
import type { VitestOptions } from './core'
6-
import type { UserConfig, VitestRunMode } from './types/config'
7+
import type { VitestRunMode } from './types/config'
78
import { resolve } from 'node:path'
89
import { slash } from '@vitest/utils'
910
import { findUp } from 'find-up'
@@ -15,7 +16,7 @@ import { createViteServer } from './vite'
1516

1617
export async function createVitest(
1718
mode: VitestRunMode,
18-
options: UserConfig,
19+
options: CliOptions,
1920
viteOverrides: ViteUserConfig = {},
2021
vitestOptions: VitestOptions = {},
2122
): Promise<Vitest> {
@@ -33,6 +34,7 @@ export async function createVitest(
3334

3435
const config: ViteInlineConfig = {
3536
configFile: configPath,
37+
configLoader: options.configLoader,
3638
// this will make "mode": "test" | "benchmark" inside defineConfig
3739
mode: options.mode || mode,
3840
plugins: await VitestPlugin(options, ctx),

packages/vitest/src/node/project.ts

+1
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ export async function initializeProject(
742742
const config: ViteInlineConfig = {
743743
...restOptions,
744744
configFile,
745+
configLoader: ctx.vite.config.inlineConfig.configLoader,
745746
// this will make "mode": "test" | "benchmark" inside defineConfig
746747
mode: options.test?.mode || options.mode || ctx.config.mode,
747748
plugins: [

pnpm-lock.yaml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cli/deps/linked/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'ok' satisfies string

test/cli/deps/linked/package.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@vitest/test-dep-linked",
3+
"type": "module",
4+
"private": true,
5+
"exports": {
6+
"./ts": "./index.ts"
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from "vitest"
2+
3+
test("browser ok", () => {})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { defineConfig } from "vitest/config"
2+
import "@vitest/test-dep-linked/ts";
3+
4+
export default defineConfig({
5+
test: {
6+
browser: {
7+
enabled: true,
8+
provider: 'playwright',
9+
headless: true,
10+
instances: [
11+
{
12+
browser: 'chromium',
13+
}
14+
]
15+
}
16+
}
17+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from "vitest"
2+
3+
test("node ok", () => {})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { defineConfig } from "vitest/config"
2+
import "@vitest/test-dep-linked/ts";
3+
4+
export default defineConfig({})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from "vitest/config"
2+
import "@vitest/test-dep-linked/ts";
3+
4+
export default defineConfig({
5+
test: {
6+
workspace: [
7+
"browser/vitest.config.ts",
8+
"node/vitest.config.ts",
9+
],
10+
},
11+
})

test/cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"@vitejs/plugin-basic-ssl": "^1.2.0",
1212
"@vitest/runner": "workspace:^",
1313
"@vitest/test-dep-error": "file:./deps/error",
14+
"@vitest/test-dep-linked": "link:./deps/linked",
1415
"@vitest/utils": "workspace:*",
1516
"debug": "^4.4.0",
1617
"unplugin-swc": "^1.5.1",

test/cli/test/config-loader.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { expect, test } from 'vitest'
2+
import { runVitestCli } from '../../test-utils'
3+
4+
test('configLoader default', async () => {
5+
const { vitest, exitCode } = await runVitestCli(
6+
'run',
7+
'--root',
8+
'fixtures/config-loader',
9+
)
10+
expect(vitest.stderr).toContain('failed to load config')
11+
expect(exitCode).not.toBe(0)
12+
})
13+
14+
test('configLoader runner', async () => {
15+
const { vitest, exitCode } = await runVitestCli(
16+
'run',
17+
'--root',
18+
'fixtures/config-loader',
19+
'--configLoader',
20+
'runner',
21+
)
22+
expect(vitest.stderr).toBe('')
23+
expect(vitest.stdout).toContain('✓ node')
24+
expect(vitest.stdout).toContain('✓ browser (chromium)')
25+
expect(exitCode).toBe(0)
26+
})

0 commit comments

Comments
 (0)