Skip to content

Commit 1c0ec34

Browse files
authored
refactor!: separate config resolution from the server creation (#10554)
1 parent b4dddb1 commit 1c0ec34

124 files changed

Lines changed: 3485 additions & 3143 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/.vitepress/config.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -618,18 +618,10 @@ export default ({ mode }: { mode: string }) => {
618618
text: 'browser.headless',
619619
link: '/config/browser/headless',
620620
},
621-
{
622-
text: 'browser.isolate',
623-
link: '/config/browser/isolate',
624-
},
625621
{
626622
text: 'browser.testerHtmlPath',
627623
link: '/config/browser/testerhtmlpath',
628624
},
629-
{
630-
text: 'browser.api',
631-
link: '/config/browser/api',
632-
},
633625
{
634626
text: 'browser.provider',
635627
link: '/config/browser/provider',

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const skipConfig = new Set([
3939
'project',
4040
'ui',
4141
'browser.name',
42-
'browser.fileParallelism',
4342
'clearCache',
4443
'tagsFilter',
4544
'listTags',

docs/api/advanced/artifacts.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ The `TestArtifactBase` interface is the base for all test artifacts.
6767
Extend this interface when creating custom test artifacts. Vitest automatically manages the `attachments` array and injects the `location` property to indicate where the artifact was created in your test code.
6868

6969
::: danger
70-
When running with [`api.allowWrite`](/config/api#api-allowwrite) or [`browser.api.allowWrite`](/config/browser/api#api-allowwrite) disabled, Vitest empties the `attachments` array on every artifact before reporting it.
70+
When running with [`api.allowWrite`](/config/api#api-allowwrite) disabled, Vitest empties the `attachments` array on every artifact before reporting it.
7171

7272
If your custom artifact narrows the `attachments` type (e.g. to a tuple), include `| []` in the union so the type reflects what actually happens at runtime.
7373
:::

docs/api/advanced/test-module.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ interface ImportDuration {
153153
function logs(): ReadonlyArray<UserConsoleLog>
154154
```
155155

156-
Console logs recorded on top level of the module during test collection.For example:
156+
Console logs recorded on top level of the module during test collection. For example:
157157

158158
```ts
159159
console.log('included') // [!code highlight]

docs/api/browser/commands.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ By default, Vitest uses `utf-8` encoding but you can override it with options.
1818
::: tip
1919
The built-in file commands follow Vite's [`server.fs`](https://vitejs.dev/config/server-options.html#server-fs-allow) restrictions for security reasons.
2020

21-
`writeFile` and `removeFile` also require write access through [`browser.api.allowWrite`](/config/browser/api) and [`api.allowWrite`](/config/api#api-allowwrite).
21+
`writeFile` and `removeFile` also require write access through [`api.allowWrite`](/config/api#api-allowwrite).
2222
:::
2323

2424
```ts
@@ -60,7 +60,7 @@ expect(input).toHaveValue('a')
6060
::: warning
6161
CDP session works only with `playwright` provider and only when using `chromium` browser. You can read more about it in playwright's [`CDPSession`](https://playwright.dev/docs/api/class-cdpsession) documentation.
6262

63-
CDP is a privileged debugging API. It is available only when browser API write and exec operations are enabled through [`browser.api.allowWrite`](/config/browser/api#api-allowwrite), [`browser.api.allowExec`](/config/browser/api#api-allowexec), [`api.allowWrite`](/config/api#api-allowwrite), and [`api.allowExec`](/config/api#api-allowexec).
63+
CDP is a privileged debugging API. It is available only when browser API write and exec operations are enabled through [`api.allowWrite`](/config/api#api-allowwrite), and [`api.allowExec`](/config/api#api-allowexec).
6464
:::
6565

6666
## Custom Commands
@@ -131,7 +131,7 @@ Custom commands run in the Vitest Node process and are callable from browser tes
131131

132132
Vitest's built-in file commands validate paths against Vite's [`server.fs`](https://vite.dev/config/server-options#server-fs-allow) restrictions and separately check whether writes are allowed. Custom commands do not automatically inherit these protections. If a custom command accepts browser-provided input and uses it to read, write, delete, execute, or expose local resources, validate that input before using it.
133133

134-
For file reads or fixture loading, use `isFileLoadingAllowed` from `vitest/node` or an explicit allowlist. For writes and deletes, also require an explicit mutation policy, such as [`browser.api.allowWrite`](/config/browser/api#api-allowwrite), [`api.allowWrite`](/config/api#api-allowwrite), and a command-specific allowed directory. For commands that execute code, shell commands, or project scripts, also check [`browser.api.allowExec`](/config/browser/api#api-allowexec) and [`api.allowExec`](/config/api#api-allowexec).
134+
For file reads or fixture loading, use `isFileLoadingAllowed` from `vitest/node` or an explicit allowlist. For writes and deletes, also require an explicit mutation policy, such as [`api.allowWrite`](/config/api#api-allowwrite), and a command-specific allowed directory. For commands that execute code, shell commands, or project scripts, also check [`api.allowExec`](/config/api#api-allowexec).
135135

136136
For example, if you create your own file-writing command instead of using Vitest's built-in `writeFile`, apply the same checks:
137137

docs/api/browser/context.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ The `cdp` export returns the current Chrome DevTools Protocol session. It is mos
212212
::: warning
213213
CDP session works only with `playwright` provider and only when using `chromium` browser. You can read more about it in playwright's [`CDPSession`](https://playwright.dev/docs/api/class-cdpsession) documentation.
214214

215-
CDP is a privileged debugging API. It is available only when browser API write and exec operations are enabled through [`browser.api.allowWrite`](/config/browser/api#api-allowwrite), [`browser.api.allowExec`](/config/browser/api#api-allowexec), [`api.allowWrite`](/config/api#api-allowwrite), and [`api.allowExec`](/config/api#api-allowexec).
215+
CDP is a privileged debugging API. It is available only when browser API write and exec operations are enabled through [`api.allowWrite`](/config/api#api-allowwrite), and [`api.allowExec`](/config/api#api-allowexec).
216216
:::
217217

218218
```ts

docs/config/api.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ outline: deep
99
- **Default:** `false`
1010
- **CLI:** `--api`, `--api.port`, `--api.host`, `--api.strictPort`
1111

12-
Listen to port and serve API for [the UI](/guide/ui) or [browser server](/guide/browser/). When set to `true`, the default port is `51204`.
12+
Listen to port and serve API for [the UI](/guide/ui) or [browser server](/guide/browser/). When set to `true`, the default port is `51204` or `63315` if running in Browser Mode.
1313

1414
## api.allowWrite <Version>4.1.0</Version> {#api-allowwrite}
1515

@@ -18,6 +18,8 @@ Listen to port and serve API for [the UI](/guide/ui) or [browser server](/guide/
1818

1919
Vitest server can save test files or snapshot files via the API. This allows anyone who can connect to the API the ability to run any arbitrary code on your machine.
2020

21+
In Browser Mode Vitest saves [annotation attachments](/guide/test-annotations), [artifacts](/api/advanced/artifacts) and [snapshots](/guide/snapshot) by receiving a WebSocket connection from the browser. This allows anyone who can connect to the API write any arbitrary code on your machine within the root of your project (configured by [`fs.allow`](https://vite.dev/config/server-options#server-fs-allow)). This option also gates privileged browser APIs that can write files indirectly, such as raw Chrome DevTools Protocol access through [`cdp()`](/api/browser/context#cdp).
22+
2123
::: danger SECURITY ADVICE
2224
Vitest does not expose the API to the internet by default and only listens on `localhost`. However if `host` is manually exposed to the network, anyone who connects to it can run arbitrary code on your machine, unless `api.allowWrite` and `api.allowExec` are set to `false`.
2325

@@ -29,4 +31,5 @@ If the host is set to anything other than `localhost` or `127.0.0.1`, Vitest wil
2931
- **Type:** `boolean`
3032
- **Default:** `true` if not exposed to the network, `false` otherwise
3133

32-
Allows running any test file via the API. See the security advice in [`api.allowWrite`](#api-allowwrite).
34+
Allows running any test file via the UI. This applies to the interactive elements (and the server code behind them) in the [UI](/guide/ui) that can run the code. This option also gates privileged browser APIs that can execute code indirectly, such as raw Chrome DevTools Protocol access through [`cdp()`](/api/browser/context#cdp).
35+

docs/config/browser/api.md

Lines changed: 0 additions & 28 deletions
This file was deleted.

docs/config/browser/isolate.md

Lines changed: 0 additions & 16 deletions
This file was deleted.

docs/guide/advanced/index.md

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,18 @@ The ["Running Tests"](/guide/advanced/tests#createvitest) guide has a usage exam
8585
function resolveConfig(
8686
options: UserConfig = {},
8787
viteOverrides: ViteUserConfig = {},
88-
): Promise<{
89-
vitestConfig: ResolvedConfig
90-
viteConfig: ResolvedViteConfig
91-
}>
88+
harness?: PluginHarness,
89+
): Promise<ResolvedViteConfig>
9290
```
9391

94-
This method resolves the config with custom parameters. If no parameters are given, the `root` will be `process.cwd()`.
92+
This method resolves the config with custom parameters, without creating a Vite server. If no parameters are given, the `root` will be `process.cwd()`.
93+
94+
It returns the resolved Vite config. The fully resolved Vitest config, including every project, lives on its `test` property.
9595

9696
```ts
9797
import { resolveConfig } from 'vitest/node'
9898
99-
// vitestConfig only has resolved "test" properties
100-
const { vitestConfig, viteConfig } = await resolveConfig({
99+
const viteConfig = await resolveConfig({
101100
mode: 'custom',
102101
configFile: false,
103102
resolve: {
@@ -108,18 +107,14 @@ const { vitestConfig, viteConfig } = await resolveConfig({
108107
pool: 'threads',
109108
},
110109
})
110+
111+
viteConfig.test.pool // 'threads'
111112
```
112113

113114
::: info
114-
Due to how Vite's `createServer` works, Vitest has to resolve the config during the plugin's `configResolve` hook. Therefore, this method is not actually used internally and is exposed exclusively as a public API.
115-
116-
If you pass down the config to the `startVitest` or `createVitest` APIs, Vitest will still resolve the config again.
117-
:::
115+
This is the same method Vitest uses internally to resolve the config before creating the server. If you pass the options down to `startVitest` or `createVitest`, Vitest resolves them again.
118116

119-
::: warning
120-
The `resolveConfig` doesn't resolve `projects`. To resolve projects configs, Vitest needs an established Vite server.
121-
122-
Also note that `viteConfig.test` will not be fully resolved. If you need Vitest config, use `vitestConfig` instead.
117+
You can pass a shared [`PluginHarness`](#pluginharness) as the third argument to reuse a logger and package installer across calls.
123118
:::
124119

125120
## parseCLI
@@ -147,3 +142,52 @@ result.options
147142
result.filter
148143
// ['./files.ts']
149144
```
145+
146+
## createCLI
147+
148+
```ts
149+
function createCLI(options?: CliParseOptions): CAC
150+
```
151+
152+
Creates the Vitest command-line interface: a [`cac`](https://github.com/cacjs/cac) instance with all of Vitest's commands and options registered. [`parseCLI`](#parsecli) is built on top of it; use `createCLI` directly if you need the raw parser.
153+
154+
```ts
155+
import { createCLI } from 'vitest/node'
156+
157+
const cli = createCLI()
158+
```
159+
160+
## PluginHarness
161+
162+
```ts
163+
class PluginHarness {
164+
vitest?: Vitest
165+
version: string
166+
logger: Logger
167+
packageInstaller: VitestPackageInstaller
168+
getVitest(): Vitest
169+
}
170+
```
171+
172+
A container that Vitest passes to its internal plugins while the config is being resolved, before a [`Vitest`](/api/advanced/vitest) instance exists. It holds the [`Logger`](#logger), the package installer and the resolved version, and exposes the `Vitest` instance via `getVitest()` once it has been created (calling it earlier throws).
173+
174+
This is an advanced, plugin-facing API. You rarely construct one directly, but you can pass a shared instance to [`resolveConfig`](#resolveconfig) to reuse a logger and package installer.
175+
176+
## Logger
177+
178+
```ts
179+
class Logger {
180+
constructor(
181+
outputStream?: Writable,
182+
errorStream?: Writable,
183+
)
184+
}
185+
```
186+
187+
Vitest's terminal logger, exposed as [`vitest.logger`](/api/advanced/vitest). It handles formatted output, the error summary, the run banner and screen clearing. Construct one with custom `stdout`/`stderr` streams to capture or redirect Vitest's output when running it programmatically.
188+
189+
```ts
190+
import { Logger } from 'vitest/node'
191+
192+
const logger = new Logger(process.stdout, process.stderr)
193+
```

0 commit comments

Comments
 (0)