Skip to content

Commit 66e648f

Browse files
authored
fix(vitest): expose provide to the public API (#5897)
1 parent 8d55d6b commit 66e648f

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

docs/advanced/api.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,45 @@ Benchmark mode calls `bench` functions and throws an error, when it encounters `
7676
### start
7777
7878
You can start running tests or benchmarks with `start` method. You can pass an array of strings to filter test files.
79+
80+
### `provide`
81+
82+
Vitest exposes `provide` method which is a shorthand for `vitest.getCoreWorkspaceProject().provide`. With this method you can pass down values from the main thread to tests. All values are checked with `structuredClone` before they are stored, but the values themselves are not cloned.
83+
84+
To recieve the values in the test, you need to import `inject` method from `vitest` entrypont:
85+
86+
```ts
87+
import { inject } from 'vitest'
88+
const port = inject('wsPort') // 3000
89+
```
90+
91+
For better type safety, we encourage you to augment the type of `ProvidedContext`:
92+
93+
```ts
94+
import { createVitest } from 'vitest/node'
95+
96+
const vitest = await createVitest('test', {
97+
watch: false,
98+
})
99+
vitest.provide('wsPort', 3000)
100+
101+
declare module 'vitest' {
102+
export interface ProvidedContext {
103+
wsPort: number
104+
}
105+
}
106+
```
107+
108+
::: warning
109+
Technically, `provide` is a method of `WorkspaceProject`, so it is limited to the specific project. However, all projects inherit the values from the core project which makes `vitest.provide` universal way of passing down values to tests.
110+
:::
111+
112+
::: tip
113+
This method is also available to [global setup files](/config/#globalsetup) for cases where you don't want to use the public API:
114+
115+
```js
116+
export default function setup({ provide }) {
117+
provide('wsPort', 3000)
118+
}
119+
```
120+
:::

docs/guide/cli-table.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@
5353
| `--browser.api.port [port]` | Specify server port. Note if the port is already being used, Vite will automatically try the next available port so this may not be the actual port the server ends up listening on. If true will be set to `63315` |
5454
| `--browser.api.host [host]` | Specify which IP addresses the server should listen on. Set this to `0.0.0.0` or `true` to listen on all addresses, including LAN and public addresses |
5555
| `--browser.api.strictPort` | Set to true to exit if port is already in use, instead of automatically trying the next available port |
56-
| `--browser.provider <name>` | Provider used to run browser tests. Some browsers are only available for specific providers. Can be "webdriverio", "playwright", or the path to a custom provider. Visit [`browser.provider`](https://vitest.dev/config/#browser-provider) for more information (default: `"webdriverio"`) |
56+
| `--browser.provider <name>` | Provider used to run browser tests. Some browsers are only available for specific providers. Can be "webdriverio", "playwright", "preview", or the path to a custom provider. Visit [`browser.provider`](https://vitest.dev/config/#browser-provider) for more information (default: `"preview"`) |
5757
| `--browser.providerOptions <options>` | Options that are passed down to a browser provider. Visit [`browser.providerOptions`](https://vitest.dev/config/#browser-provideroptions) for more information |
5858
| `--browser.isolate` | Run every browser test file in isolation. To disable isolation, use `--browser.isolate=false` (default: `true`) |
5959
| `--browser.ui` | Show Vitest UI when running tests (default: `!process.env.CI`) |
60+
| `--browser.fileParallelism` | Should browser test files run in parallel. Use `--browser.fileParallelism=false` to disable (default: `true`) |
6061
| `--pool <pool>` | Specify pool, if not running in the browser (default: `threads`) |
6162
| `--poolOptions.threads.isolate` | Isolate tests in threads pool (default: `true`) |
6263
| `--poolOptions.threads.singleThread` | Run tests inside a single thread (default: `false`) |
@@ -117,6 +118,7 @@
117118
| `--expect.requireAssertions` | Require that all tests have at least one assertion |
118119
| `--expect.poll.interval <interval>` | Poll interval in milliseconds for `expect.poll()` assertions (default: `50`) |
119120
| `--expect.poll.timeout <timeout>` | Poll timeout in milliseconds for `expect.poll()` assertions (default: `1000`) |
121+
| `--printConsoleTrace` | Always print console stack traces |
120122
| `--run` | Disable watch mode |
121123
| `--no-color` | Removes colors from the console output |
122124
| `--clearScreen` | Clear terminal screen when re-running tests during watch mode (default: `true`) |

packages/vitest/src/node/core.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { CancelReason, File, TaskResultPack } from '@vitest/runner'
1313
import { ViteNodeServer } from 'vite-node/server'
1414
import type { defineWorkspace } from 'vitest/config'
1515
import { version } from '../../package.json' with { type: 'json' }
16-
import type { ArgumentsType, CoverageProvider, OnServerRestartHandler, Reporter, ResolvedConfig, SerializableSpec, UserConfig, UserConsoleLog, UserWorkspaceConfig, VitestRunMode } from '../types'
16+
import type { ArgumentsType, CoverageProvider, OnServerRestartHandler, ProvidedContext, Reporter, ResolvedConfig, SerializableSpec, UserConfig, UserConsoleLog, UserWorkspaceConfig, VitestRunMode } from '../types'
1717
import { getTasks, hasFailed, noop, slash, toArray, wildcardPatternToRegExp } from '../utils'
1818
import { getCoverageProvider } from '../integrations/coverage'
1919
import { CONFIG_NAMES, configFiles, workspacesFiles as workspaceFiles } from '../constants'
@@ -180,6 +180,10 @@ export class Vitest {
180180
this.configOverride.testNamePattern = this.config.testNamePattern
181181
}
182182

183+
public provide<T extends keyof ProvidedContext>(key: T, value: ProvidedContext[T]) {
184+
this.getCoreWorkspaceProject().provide(key, value)
185+
}
186+
183187
private async createCoreProject() {
184188
this.coreWorkspaceProject = await WorkspaceProject.createCoreProject(this)
185189
return this.coreWorkspaceProject

0 commit comments

Comments
 (0)