You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/advanced/test-project.md
+9-1Lines changed: 9 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -98,7 +98,15 @@ It is based on the root of the project and its name. Note that the root path is
98
98
99
99
## vite
100
100
101
-
This is project's [`ViteDevServer`](https://vite.dev/guide/api-javascript#vitedevserver). All projects have their own Vite servers.
101
+
This is project's [`ViteDevServer`](https://vite.dev/guide/api-javascript#vitedevserver). Note that the server is not necessarily exclusive to this project: other projects can reuse it when the [`sharedViteServer`](/config/sharedviteserver) option applies, and browser instances of the same cluster share a single browser server.
102
+
103
+
## sharedViteServer
104
+
105
+
```ts
106
+
const sharedViteServer:boolean
107
+
```
108
+
109
+
`true` when the project reuses the Vite server of the config that declared it instead of resolving its own (see the [`sharedViteServer`](/config/sharedviteserver) option). The project that owns the server reports `false` even when other projects reuse it. To detect any two projects sharing a server (including browser instances), compare their [`vite`](#vite) references.
Inline [projects](/guide/projects) that don't modify the Vite config reuse the Vite server of the config that declares them. Instead of resolving a new Vite config and creating a new server for every project, such projects share the declaring config's server and its transform cache, so shared source files are transformed once instead of once per project and tests run faster. The performance improvement varies depending on the number of inline projects and how many source files they have in common.
13
+
14
+
This option _only_ applies to inline projects. Projects referenced as config files or directories always resolve their own Vite config and create their own server.
15
+
16
+
A project still gets its own Vite server when it defines Vite-level options that change the server (`plugins`, `resolve`, and so on), when its `extends` doesn't point to the declaring config, or when it defines test options that affect the Vite config:
Options like `env`, `setupFiles`, `server.deps`, or `environment` don't prevent sharing: every project keeps its own module resolution rules, module runner, and module instances on top of the shared server.
27
+
28
+
The same applies to Vite-level values that don't change the server: an empty `plugins` list (`plugins: isCI ? [ciPlugin()] : []`) and `define`.
To see the decision for every project, including why a project resolves its own server, run Vitest with `DEBUG=vitest:projects`. API consumers can check whether a project reuses the declaring config's server via [`project.sharedViteServer`](/api/advanced/test-project#sharedviteserver).
69
+
70
+
The option applies to every level: inline projects of a [nested projects container](/guide/projects#nested-projects) share the container's server the same way.
71
+
72
+
::: warning
73
+
When projects share a server, the declaring config file is executed once instead of once per project. Plugins are instantiated once, and their `config` hooks cannot observe per-project test options. If a plugin needs to behave differently per project, disable this option or don't share the server for that project (for example, set `extends: false` or define the project in its own config file).
Copy file name to clipboardExpand all lines: docs/guide/migration.md
+26Lines changed: 26 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -157,6 +157,32 @@ Since the inherited `projects` paths resolve relative to the referenced config,
157
157
158
158
Inline configurations continue to ignore the `projects` field at runtime, but it is now also excluded from their `ProjectConfig` type.
159
159
160
+
### Inline Projects Share the Vite Server by Default
161
+
162
+
Inline projects that don't modify the Vite config now reuse the Vite server of the config that declares them instead of resolving a new Vite config and creating a new server per project. This is controlled by the new [`sharedViteServer`](/config/sharedviteserver) option, which is enabled by default.
163
+
164
+
Sharing the server means files are transformed once instead of once per project, so tests should now run faster: a config with several inline projects over the same codebase benefits the most, while a config with a single project won't see a difference.
165
+
166
+
Note that this _only_ applies to inline projects. Projects referenced as config files or directories always resolve their own Vite config and create their own server, exactly as before.
167
+
168
+
An inline project also still gets its own Vite server when it defines Vite-level options that change the server (`plugins`, `resolve`, and so on), a non-default `extends`, or test options that affect the Vite config: `alias`, `browser`, `css`, `deps.moduleDirectories`, `deps.optimizer`, `mode`, or `root`. Every project keeps its own module resolution rules, module runner, and module instances, so options like `env`, `setupFiles`, `server.deps`, or `environment` still resolve per project.
169
+
170
+
The observable change: when the server is shared, the declaring config file is executed once instead of once per project, so its plugins are instantiated once and their `config` hooks no longer run for every project. If a plugin relies on being re-instantiated per project, disable the sharing:
171
+
172
+
```ts [vitest.config.ts]
173
+
import { defineConfig } from'vitest/config'
174
+
175
+
exportdefaultdefineConfig({
176
+
test: {
177
+
sharedViteServer: false, // [!code ++]
178
+
projects: [
179
+
{ test: { name: 'unit' } },
180
+
{ test: { name: 'integration' } },
181
+
],
182
+
},
183
+
})
184
+
```
185
+
160
186
### Hoisted Mocking Calls Must Be at the Top Level
161
187
162
188
[`vi.mock`](/api/vi#vi-mock), [`vi.unmock`](/api/vi#vi-unmock), and [`vi.hoisted`](/api/vi#vi-hoisted) are hoisted to the top of the file and run before any surrounding code. Calling them inside a function, block, or `describe`/`test` callback previously only logged a warning. Vitest 5.0 now throws, because the call does not execute where it is written:
0 commit comments