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/config/index.md
+49-3Lines changed: 49 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1003,6 +1003,54 @@ afterEach(() => {
1003
1003
globalThis.resetBeforeEachTest=true
1004
1004
```
1005
1005
1006
+
### provide <Version>2.1.0</Version> {#provide}
1007
+
1008
+
-**Type:**`Partial<ProvidedContext>`
1009
+
1010
+
Define values that can be accessed inside your tests using `inject` method.
1011
+
1012
+
:::code-group
1013
+
```ts [vitest.config.js]
1014
+
import { defineConfig } from'vitest/config'
1015
+
1016
+
exportdefaultdefineConfig({
1017
+
test: {
1018
+
provide: {
1019
+
API_KEY: '123',
1020
+
},
1021
+
},
1022
+
})
1023
+
```
1024
+
```ts [my.test.js]
1025
+
import { expect, inject, test } from'vitest'
1026
+
1027
+
test('api key is defined', () => {
1028
+
expect(inject('API_KEY')).toBe('123')
1029
+
})
1030
+
```
1031
+
:::
1032
+
1033
+
::: warning
1034
+
Properties have to be strings and values need to be [serializable](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#supported_types) because this object will be transferred between different processes.
1035
+
:::
1036
+
1037
+
::: tip
1038
+
If you are using TypeScript, you will need to augment `ProvidedContext` type for type safe access:
1039
+
1040
+
```ts
1041
+
// vitest.shims.d.ts
1042
+
1043
+
declaremodule'vitest' {
1044
+
exportinterfaceProvidedContext {
1045
+
API_KEY:string
1046
+
}
1047
+
}
1048
+
1049
+
// mark this file as a module so augmentation works correctly
1050
+
export {}
1051
+
```
1052
+
:::
1053
+
1006
1054
### globalSetup
1007
1055
1008
1056
-**Type:**`string | string[]`
@@ -1018,7 +1066,7 @@ Multiple globalSetup files are possible. setup and teardown are executed sequent
1018
1066
::: warning
1019
1067
Global setup runs only if there is at least one running test. This means that global setup might start running during watch mode after test file is changed (the test file will wait for global setup to finish before running).
1020
1068
1021
-
Beware that the global setup is running in a different global scope, so your tests don't have access to variables defined here. However, you can pass down serializable data to tests via `provide` method:
1069
+
Beware that the global setup is running in a different global scope, so your tests don't have access to variables defined here. However, you can pass down serializable data to tests via [`provide`](#provide) method:
1022
1070
1023
1071
:::code-group
1024
1072
```js [globalSetup.js]
@@ -1033,8 +1081,6 @@ export default function setup({ provide }: GlobalSetupContext) {
1033
1081
provide('wsPort', 3000)
1034
1082
}
1035
1083
1036
-
// You can also extend `ProvidedContext` type
1037
-
// to have type safe access to `provide/inject` methods:
0 commit comments