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
# Plugin API <Version>3.1.0</Version> {#plugin-api}
7
+
8
+
::: warning
9
+
This is an advanced API. If you just want to [run tests](/guide/), you probably don't need this. It is primarily used by library authors.
10
+
11
+
This guide assumes you know how to work with [Vite plugins](https://vite.dev/guide/api-plugin.html).
12
+
:::
13
+
14
+
Vitest supports an experimental `configureVitest`[plugin](https://vite.dev/guide/api-plugin.html) hook hook since version 3.1. Any feedback regarding this API is welcome in [GitHub](https://github.com/vitest-dev/vitest/discussions/7104).
Vitest re-exports all Vite type-only imports via a `Vite` namespace, which you can use to keep your versions in sync. However, if you are writing a plugin for both Vite and Vitest, you can continue using the `Plugin` type from the `vite` entrypoint. Just make sure you have `vitest/config` referenced somewhere so that `configureVitest` is augmented correctly:
50
+
51
+
```ts
52
+
/// <referencetypes="vitest/config" />
53
+
```
54
+
:::
55
+
56
+
Unlike [`reporter.onInit`](/advanced/api/reporters#oninit), this hooks runs early in Vitest lifecycle allowing you to make changes to configuration like `coverage` and `reporters`. A more notable change is that you can manipulate the global config from a [workspace project](/guide/workspace) if your plugin is defined in the project and not in the global config.
57
+
58
+
## Context
59
+
60
+
### project
61
+
62
+
The current [test project](./test-project) that the plugin belongs to.
63
+
64
+
::: warning Browser Mode
65
+
Note that if you are relying on a browser feature, the `project.browser` field is not set yet. Use [`reporter.onBrowserInit`](./reporters#onbrowserinit) event instead.
66
+
:::
67
+
68
+
### vitest
69
+
70
+
The global [Vitest](./vitest) instance. You can change the global configuration by directly mutating the `vitest.config` property:
Note that Vitest already resolved the config, so some types might be different from the usual user configuration. This also means that some properties will not be resolved again, like `setupFile`. If you are adding new files, make sure to resolve it first.
79
+
80
+
At this point reporters are not created yet, so modifying `vitest.reporters` will have no effect because it will be overwritten. If you need to inject your own reporter, modify the config instead.
Theproject's `configFile` can be accessed in Vite'sconfig: `project.vite.config.configFile`.
123
+
124
+
Notethatthiswillalsoinheritthe`name`-Vitestdoesn't allow multiple projects with the same name, so this will throw an error. Make sure you specified a different name. You can access the current name via the `project.name` property and all used names are available in the `vitest.projects` array.
thrownewError(`The ${name} was not set. It means that \`vitest.${property}\` was called before the Vite server was established. Either await the Vitest promise or check that it is initialized with \`vitest.ready()\` before accessing \`vitest.${property}\`.`)
1299
+
thrownewError(`The ${name} was not set. It means that \`vitest.${property}\` was called before the Vite server was established. Await the Vitest promise before accessing \`vitest.${property}\`.`)
test('adding a plugin with existing name throws and error',async()=>{
176
+
awaitexpect(()=>vitest({
177
+
workspace: [
178
+
{
179
+
test: {
180
+
name: 'project-1',
181
+
},
182
+
plugins: [
183
+
{
184
+
name: 'test',
185
+
asyncconfigureVitest({ injectTestProjects }){
186
+
awaitinjectTestProjects({
187
+
test: {
188
+
name: 'project-1',
189
+
},
190
+
})
191
+
},
192
+
},
193
+
],
194
+
},
195
+
],
196
+
}),
197
+
).rejects.toThrowError('Project name "project-1" is not unique. All projects in a workspace should have unique names. Make sure your configuration is correct.')
198
+
199
+
awaitexpect(()=>vitest({
200
+
workspace: [
201
+
{
202
+
plugins: [
203
+
{
204
+
name: 'test',
205
+
asyncconfigureVitest({ injectTestProjects }){
206
+
awaitinjectTestProjects({
207
+
test: {
208
+
name: 'project-1',
209
+
},
210
+
})
211
+
awaitinjectTestProjects({
212
+
test: {
213
+
name: 'project-1',
214
+
},
215
+
})
216
+
},
217
+
},
218
+
],
219
+
},
220
+
],
221
+
}),
222
+
).rejects.toThrowError('Project name "project-1" is not unique. All projects in a workspace should have unique names. Make sure your configuration is correct.')
223
+
224
+
awaitexpect(()=>vitest({
225
+
workspace: [
226
+
{
227
+
plugins: [
228
+
{
229
+
name: 'test',
230
+
asyncconfigureVitest({ injectTestProjects }){
231
+
awaitinjectTestProjects([
232
+
{
233
+
test: {
234
+
name: 'project-1',
235
+
},
236
+
},
237
+
{
238
+
test: {
239
+
name: 'project-1',
240
+
},
241
+
},
242
+
])
243
+
},
244
+
},
245
+
],
246
+
},
247
+
],
248
+
}),
249
+
).rejects.toThrowError('Project name "project-1" is not unique. All projects in a workspace should have unique names. Make sure your configuration is correct.')
0 commit comments