Skip to content

Commit 0dc93ea

Browse files
authored
feat: introduce separate packages for browser mode providers (#8629)
1 parent 88e62c7 commit 0dc93ea

File tree

217 files changed

+2757
-1715
lines changed

Some content is hidden

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

217 files changed

+2757
-1715
lines changed

docs/.vitepress/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ export default ({ mode }: { mode: string }) => {
253253
link: '/guide/browser/webdriverio',
254254
docFooterText: 'Configuring WebdriverIO | Browser Mode',
255255
},
256+
{
257+
text: 'Configuring Preview',
258+
link: '/guide/browser/preview',
259+
docFooterText: 'Configuring Preview | Browser Mode',
260+
},
256261
],
257262
},
258263
{

docs/guide/browser/assertion-api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ title: Assertion API | Browser Mode
77
Vitest provides a wide range of DOM assertions out of the box forked from [`@testing-library/jest-dom`](https://github.com/testing-library/jest-dom) library with the added support for locators and built-in retry-ability.
88

99
::: tip TypeScript Support
10-
If you are using [TypeScript](/guide/browser/#typescript) or want to have correct type hints in `expect`, make sure you have `@vitest/browser/context` referenced somewhere. If you never imported from there, you can add a `reference` comment in any file that's covered by your `tsconfig.json`:
10+
If you are using [TypeScript](/guide/browser/#typescript) or want to have correct type hints in `expect`, make sure you have `vitest/browser` referenced somewhere. If you never imported from there, you can add a `reference` comment in any file that's covered by your `tsconfig.json`:
1111

1212
```ts
13-
/// <reference types="@vitest/browser/context" />
13+
/// <reference types="vitest/browser" />
1414
```
1515
:::
1616

1717
Tests in the browser might fail inconsistently due to their asynchronous nature. Because of this, it is important to have a way to guarantee that assertions succeed even if the condition is delayed (by a timeout, network request, or animation, for example). For this purpose, Vitest provides retriable assertions out of the box via the [`expect.poll`](/api/expect#poll) and `expect.element` APIs:
1818

1919
```ts
2020
import { expect, test } from 'vitest'
21-
import { page } from '@vitest/browser/context'
21+
import { page } from 'vitest/browser'
2222

2323
test('error banner is rendered', async () => {
2424
triggerError()

docs/guide/browser/commands.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This API follows [`server.fs`](https://vitejs.dev/config/server-options.html#ser
2020
:::
2121

2222
```ts
23-
import { server } from '@vitest/browser/context'
23+
import { server } from 'vitest/browser'
2424

2525
const { readFile, writeFile, removeFile } = server.commands
2626

@@ -38,10 +38,10 @@ it('handles files', async () => {
3838

3939
## CDP Session
4040

41-
Vitest exposes access to raw Chrome Devtools Protocol via the `cdp` method exported from `@vitest/browser/context`. It is mostly useful to library authors to build tools on top of it.
41+
Vitest exposes access to raw Chrome Devtools Protocol via the `cdp` method exported from `vitest/browser`. It is mostly useful to library authors to build tools on top of it.
4242

4343
```ts
44-
import { cdp } from '@vitest/browser/context'
44+
import { cdp } from 'vitest/browser'
4545

4646
const input = document.createElement('input')
4747
document.body.appendChild(input)
@@ -97,10 +97,10 @@ export default function BrowserCommands(): Plugin {
9797
}
9898
```
9999

100-
Then you can call it inside your test by importing it from `@vitest/browser/context`:
100+
Then you can call it inside your test by importing it from `vitest/browser`:
101101

102102
```ts
103-
import { commands } from '@vitest/browser/context'
103+
import { commands } from 'vitest/browser'
104104
import { expect, test } from 'vitest'
105105

106106
test('custom command works correctly', async () => {
@@ -109,7 +109,7 @@ test('custom command works correctly', async () => {
109109
})
110110

111111
// if you are using TypeScript, you can augment the module
112-
declare module '@vitest/browser/context' {
112+
declare module 'vitest/browser' {
113113
interface BrowserCommands {
114114
myCustomCommand: (arg1: string, arg2: string) => Promise<{
115115
someValue: true

docs/guide/browser/component-testing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ The key is using `page.elementLocator()` to bridge Testing Library's DOM output
138138
```jsx
139139
// For Solid.js components
140140
import { render } from '@testing-library/solid'
141-
import { page } from '@vitest/browser/context'
141+
import { page } from 'vitest/browser'
142142

143143
test('Solid component handles user interaction', async () => {
144144
// Use Testing Library to render the component
@@ -564,7 +564,7 @@ import { render } from 'vitest-browser-react' // [!code ++]
564564
### Key Differences
565565
566566
- Use `await expect.element()` instead of `expect()` for DOM assertions
567-
- Use `@vitest/browser/context` for user interactions instead of `@testing-library/user-event`
567+
- Use `vitest/browser` for user interactions instead of `@testing-library/user-event`
568568
- Browser Mode provides real browser environment for accurate testing
569569
570570
## Learn More

docs/guide/browser/config.md

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ You can change the browser configuration by updating the `test.browser` field in
44

55
```ts [vitest.config.ts]
66
import { defineConfig } from 'vitest/config'
7-
import { playwright } from '@vitest/browser/providers/playwright'
7+
import { playwright } from '@vitest/browser-playwright'
88

99
export default defineConfig({
1010
test: {
@@ -47,14 +47,11 @@ Run all tests inside a browser by default. Note that `--browser` only works if y
4747
## browser.instances
4848

4949
- **Type:** `BrowserConfig`
50-
- **Default:** `[{ browser: name }]`
51-
52-
Defines multiple browser setups. Every config has to have at least a `browser` field. The config supports your providers configurations:
50+
- **Default:** `[]`
5351

54-
- [Configuring Playwright](/guide/browser/playwright)
55-
- [Configuring WebdriverIO](/guide/browser/webdriverio)
52+
Defines multiple browser setups. Every config has to have at least a `browser` field.
5653

57-
In addition to that, you can also specify most of the [project options](/config/) (not marked with a <NonProjectOption /> icon) and some of the `browser` options like `browser.testerHtmlPath`.
54+
You can specify most of the [project options](/config/) (not marked with a <NonProjectOption /> icon) and some of the `browser` options like `browser.testerHtmlPath`.
5855

5956
::: warning
6057
Every browser config inherits options from the root config:
@@ -79,8 +76,6 @@ export default defineConfig({
7976
})
8077
```
8178

82-
During development, Vitest supports only one [non-headless](#browser-headless) configuration. You can limit the headed project yourself by specifying `headless: false` in the config, or by providing the `--browser.headless=false` flag, or by filtering projects with `--project=chromium` flag.
83-
8479
For more examples, refer to the ["Multiple Setups" guide](/guide/browser/multiple-setups).
8580
:::
8681

@@ -94,8 +89,6 @@ List of available `browser` options:
9489
- [`browser.screenshotFailures`](#browser-screenshotfailures)
9590
- [`browser.provider`](#browser-provider)
9691

97-
By default, Vitest creates an array with a single element which uses the [`browser.name`](#browser-name) field as a `browser`. Note that this behaviour will be removed with Vitest 4.
98-
9992
Under the hood, Vitest transforms these instances into separate [test projects](/advanced/api/test-project) sharing a single Vite server for better caching performance.
10093

10194
## browser.headless
@@ -134,12 +127,12 @@ Configure options for Vite server that serves code in the browser. Does not affe
134127
- **Default:** `'preview'`
135128
- **CLI:** `--browser.provider=playwright`
136129

137-
The return value of the provider factory. You can import the factory from `@vitest/browser/providers/<provider-name>` or make your own provider:
130+
The return value of the provider factory. You can import the factory from `@vitest/browser-<provider-name>` or make your own provider:
138131

139132
```ts{8-10}
140-
import { playwright } from '@vitest/browser/providers/playwright'
141-
import { webdriverio } from '@vitest/browser/providers/webdriverio'
142-
import { preview } from '@vitest/browser/providers/preview'
133+
import { playwright } from '@vitest/browser-playwright'
134+
import { webdriverio } from '@vitest/browser-webdriverio'
135+
import { preview } from '@vitest/browser-preview'
143136
144137
export default defineConfig({
145138
test: {
@@ -155,7 +148,7 @@ export default defineConfig({
155148
To configure how provider initializes the browser, you can pass down options to the factory function:
156149

157150
```ts{7-13,20-26}
158-
import { playwright } from '@vitest/browser/providers/playwright'
151+
import { playwright } from '@vitest/browser-playwright'
159152
160153
export default defineConfig({
161154
test: {
@@ -294,7 +287,7 @@ export interface BrowserScript {
294287
- **Type:** `Record<string, BrowserCommand>`
295288
- **Default:** `{ readFile, writeFile, ... }`
296289

297-
Custom [commands](/guide/browser/commands) that can be imported during browser tests from `@vitest/browser/commands`.
290+
Custom [commands](/guide/browser/commands) that can be imported during browser tests from `vitest/browser`.
298291

299292
## browser.connectTimeout
300293

docs/guide/browser/context.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Context API | Browser Mode
44

55
# Context API
66

7-
Vitest exposes a context module via `@vitest/browser/context` entry point. As of 2.0, it exposes a small set of utilities that might be useful to you in tests.
7+
Vitest exposes a context module via `vitest/browser` entry point. As of 2.0, it exposes a small set of utilities that might be useful to you in tests.
88

99
## `userEvent`
1010

docs/guide/browser/index.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ To activate browser mode in your Vitest configuration, set the `browser.enabled`
9999

100100
```ts [vitest.config.ts]
101101
import { defineConfig } from 'vitest/config'
102-
import { playwright } from '@vitest/browser/providers/playwright'
102+
import { playwright } from '@vitest/browser-playwright'
103103

104104
export default defineConfig({
105105
test: {
@@ -127,7 +127,7 @@ If you have not used Vite before, make sure you have your framework's plugin ins
127127
```ts [react]
128128
import { defineConfig } from 'vitest/config'
129129
import react from '@vitejs/plugin-react'
130-
import { playwright } from '@vitest/browser/providers/playwright'
130+
import { playwright } from '@vitest/browser-playwright'
131131

132132
export default defineConfig({
133133
plugins: [react()],
@@ -144,7 +144,7 @@ export default defineConfig({
144144
```
145145
```ts [vue]
146146
import { defineConfig } from 'vitest/config'
147-
import { playwright } from '@vitest/browser/providers/playwright'
147+
import { playwright } from '@vitest/browser-playwright'
148148
import vue from '@vitejs/plugin-vue'
149149

150150
export default defineConfig({
@@ -163,7 +163,7 @@ export default defineConfig({
163163
```ts [svelte]
164164
import { defineConfig } from 'vitest/config'
165165
import { svelte } from '@sveltejs/vite-plugin-svelte'
166-
import { playwright } from '@vitest/browser/providers/playwright'
166+
import { playwright } from '@vitest/browser-playwright'
167167

168168
export default defineConfig({
169169
plugins: [svelte()],
@@ -181,7 +181,7 @@ export default defineConfig({
181181
```ts [solid]
182182
import { defineConfig } from 'vitest/config'
183183
import solidPlugin from 'vite-plugin-solid'
184-
import { playwright } from '@vitest/browser/providers/playwright'
184+
import { playwright } from '@vitest/browser-playwright'
185185

186186
export default defineConfig({
187187
plugins: [solidPlugin()],
@@ -199,7 +199,7 @@ export default defineConfig({
199199
```ts [marko]
200200
import { defineConfig } from 'vitest/config'
201201
import marko from '@marko/vite'
202-
import { playwright } from '@vitest/browser/providers/playwright'
202+
import { playwright } from '@vitest/browser-playwright'
203203

204204
export default defineConfig({
205205
plugins: [marko()],
@@ -217,7 +217,7 @@ export default defineConfig({
217217
```ts [qwik]
218218
import { defineConfig } from 'vitest/config'
219219
import { qwikVite } from '@builder.io/qwik/optimizer'
220-
import { playwright } from '@vitest/browser/providers/playwright'
220+
import { playwright } from '@vitest/browser-playwright'
221221

222222
// optional, run the tests in SSR mode
223223
import { testSSR } from 'vitest-browser-qwik/ssr-plugin'
@@ -241,7 +241,7 @@ If you need to run some tests using Node-based runner, you can define a [`projec
241241

242242
```ts [vitest.config.ts]
243243
import { defineConfig } from 'vitest/config'
244-
import { playwright } from '@vitest/browser/providers/playwright'
244+
import { playwright } from '@vitest/browser-playwright'
245245

246246
export default defineConfig({
247247
test: {
@@ -338,7 +338,7 @@ Here's an example configuration enabling headless mode:
338338

339339
```ts [vitest.config.ts]
340340
import { defineConfig } from 'vitest/config'
341-
import { playwright } from '@vitest/browser/providers/playwright'
341+
import { playwright } from '@vitest/browser-playwright'
342342

343343
export default defineConfig({
344344
test: {
@@ -369,7 +369,7 @@ By default, you don't need any external packages to work with the Browser Mode:
369369

370370
```js [example.test.js]
371371
import { expect, test } from 'vitest'
372-
import { page } from '@vitest/browser/context'
372+
import { page } from 'vitest/browser'
373373
import { render } from './my-render-function.js'
374374

375375
test('properly handles form inputs', async () => {
@@ -407,15 +407,15 @@ Besides rendering components and locating elements, you will also need to make a
407407

408408
```ts
409409
import { expect } from 'vitest'
410-
import { page } from '@vitest/browser/context'
410+
import { page } from 'vitest/browser'
411411
// element is rendered correctly
412412
await expect.element(page.getByText('Hello World')).toBeInTheDocument()
413413
```
414414

415-
Vitest exposes a [Context API](/guide/browser/context) with a small set of utilities that might be useful to you in tests. For example, if you need to make an interaction, like clicking an element or typing text into an input, you can use `userEvent` from `@vitest/browser/context`. Read more at the [Interactivity API](/guide/browser/interactivity-api).
415+
Vitest exposes a [Context API](/guide/browser/context) with a small set of utilities that might be useful to you in tests. For example, if you need to make an interaction, like clicking an element or typing text into an input, you can use `userEvent` from `vitest/browser`. Read more at the [Interactivity API](/guide/browser/interactivity-api).
416416

417417
```ts
418-
import { page, userEvent } from '@vitest/browser/context'
418+
import { page, userEvent } from 'vitest/browser'
419419
await userEvent.fill(page.getByLabelText(/username/i), 'Alice')
420420
// or just locator.fill
421421
await page.getByLabelText(/username/i).fill('Alice')
@@ -532,7 +532,7 @@ For unsupported frameworks, we recommend using `testing-library` packages:
532532
You can also see more examples in [`browser-examples`](https://github.com/vitest-tests/browser-examples) repository.
533533

534534
::: warning
535-
`testing-library` provides a package `@testing-library/user-event`. We do not recommend using it directly because it simulates events instead of actually triggering them - instead, use [`userEvent`](/guide/browser/interactivity-api) imported from `@vitest/browser/context` that uses Chrome DevTools Protocol or Webdriver (depending on the provider) under the hood.
535+
`testing-library` provides a package `@testing-library/user-event`. We do not recommend using it directly because it simulates events instead of actually triggering them - instead, use [`userEvent`](/guide/browser/interactivity-api) imported from `vitest/browser` that uses Chrome DevTools Protocol or Webdriver (depending on the provider) under the hood.
536536
:::
537537

538538
::: code-group

0 commit comments

Comments
 (0)