Skip to content

Commit fe3190d

Browse files
docs: rearrange browser guide (#5799)
Co-authored-by: Vladimir Sheremet <[email protected]>
1 parent 7afb368 commit fe3190d

File tree

1 file changed

+92
-35
lines changed

1 file changed

+92
-35
lines changed

docs/guide/browser.md

+92-35
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,93 @@ title: Browser Mode | Guide
66

77
This page provides information about the experimental browser mode feature in the Vitest API, which allows you to run your tests in the browser natively, providing access to browser globals like window and document. This feature is currently under development, and APIs may change in the future.
88

9+
## Installation
10+
11+
By default, Browser Mode doesn't require any provider to run tests locally because it reuses your existing browser.
12+
13+
::: code-group
14+
```bash [npm]
15+
npm install -D vitest @vitest/browser
16+
```
17+
```bash [yarn]
18+
yarn add -D vitest @vitest/browser
19+
```
20+
```bash [pnpm]
21+
pnpm add -D vitest @vitest/browser
22+
```
23+
```bash [bun]
24+
bun add -D vitest @vitest/browser
25+
```
26+
:::
27+
28+
::: warning
29+
However, to run tests in CI you need to install either [`playwright`](https://npmjs.com/package/playwright) or [`webdriverio`](https://www.npmjs.com/package/webdriverio). We also recommend switching to either one of them for testing locally instead of using the default `none` provider since it relies on simulating events instead of using Chrome DevTools Protocol.
30+
:::
31+
32+
### Using Playwright
33+
34+
::: code-group
35+
```bash [npm]
36+
npm install -D vitest @vitest/browser playwright
37+
```
38+
```bash [yarn]
39+
yarn add -D vitest @vitest/browser playwright
40+
```
41+
```bash [pnpm]
42+
pnpm add -D vitest @vitest/browser playwright
43+
```
44+
```bash [bun]
45+
bun add -D vitest @vitest/browser playwright
46+
```
47+
:::
48+
49+
### Using Webdriverio
50+
51+
::: code-group
52+
```bash [npm]
53+
npm install -D vitest @vitest/browser webdriverio
54+
```
55+
```bash [yarn]
56+
yarn add -D vitest @vitest/browser webdriverio
57+
```
58+
```bash [pnpm]
59+
pnpm add -D vitest @vitest/browser webdriverio
60+
```
61+
```bash [bun]
62+
bun add -D vitest @vitest/browser webdriverio
63+
```
64+
:::
65+
66+
## Configuration
67+
68+
To activate browser mode in your Vitest configuration, you can use the `--browser` flag or set the `browser.enabled` field to `true` in your Vitest configuration file. Here is an example configuration using the browser field:
69+
70+
```ts
71+
export default defineConfig({
72+
test: {
73+
browser: {
74+
provider: 'playwright', // or 'webdriverio'
75+
enabled: true,
76+
name: 'chrome', // browser name is required
77+
},
78+
}
79+
})
80+
```
81+
82+
## Browser Option Types
83+
84+
The browser option in Vitest depends on the provider. Vitest will fail, if you pass `--browser` and don't specify its name in the config file. Available options:
85+
86+
- `webdriverio` supports these browsers:
87+
- `firefox`
88+
- `chrome`
89+
- `edge`
90+
- `safari`
91+
- `playwright` supports these browsers:
92+
- `firefox`
93+
- `webkit`
94+
- `chromium`
95+
996
## Browser Compatibility
1097

1198
Vitest uses [Vite dev server](https://vitejs.dev/guide/#browser-support) to run your tests, so we only support features specified in the [`esbuild.target`](https://vitejs.dev/config/shared-options.html#esbuild) option (`esnext` by default).
@@ -43,35 +130,6 @@ The browser mode feature of Vitest is still in its early stages of development.
43130

44131
Vitest browser requires spinning up the provider and the browser during the initialization process, which can take some time. This can result in longer initialization times compared to other testing patterns.
45132

46-
## Configuration
47-
48-
To activate browser mode in your Vitest configuration, you can use the `--browser` flag or set the `browser.enabled` field to `true` in your Vitest configuration file. Here is an example configuration using the browser field:
49-
50-
```ts
51-
export default defineConfig({
52-
test: {
53-
browser: {
54-
enabled: true,
55-
name: 'chrome', // browser name is required
56-
},
57-
}
58-
})
59-
```
60-
61-
## Browser Option Types
62-
63-
The browser option in Vitest depends on the provider. Vitest will fail, if you pass `--browser` and don't specify its name in the config file. Available options:
64-
65-
- `webdriverio` (default) supports these browsers:
66-
- `firefox`
67-
- `chrome`
68-
- `edge`
69-
- `safari`
70-
- `playwright` supports these browsers:
71-
- `firefox`
72-
- `webkit`
73-
- `chromium`
74-
75133
## Cross-Browser Testing
76134

77135
When you specify a browser name in the browser option, Vitest will try to run the specified browser using [WebdriverIO](https://webdriver.io/) by default, and then run the tests there. This feature makes cross-browser testing easy to use and configure in environments like a CI. If you don't want to use WebdriverIO, you can configure the custom browser provider by using `browser.provider` option.
@@ -88,12 +146,6 @@ Or you can provide browser options to CLI with dot notation:
88146
npx vitest --browser.name=chrome --browser.headless
89147
```
90148

91-
::: tip NOTE
92-
When using the Safari browser option with WebdriverIO, the `safaridriver` needs to be activated by running `sudo safaridriver --enable` on your device.
93-
94-
Additionally, when running your tests, Vitest will attempt to install some drivers for compatibility with `safaridriver`.
95-
:::
96-
97149
## Headless
98150

99151
Headless mode is another option available in the browser mode. In headless mode, the browser runs in the background without a user interface, which makes it useful for running automated tests. The headless option in Vitest can be set to a boolean value to enable or disable headless mode.
@@ -104,6 +156,7 @@ Here's an example configuration enabling headless mode:
104156
export default defineConfig({
105157
test: {
106158
browser: {
159+
provider: 'playwright',
107160
enabled: true,
108161
headless: true,
109162
},
@@ -119,6 +172,10 @@ npx vitest --browser.name=chrome --browser.headless
119172

120173
In this case, Vitest will run in headless mode using the Chrome browser.
121174

175+
::: warning
176+
Headless mode is not available by default. You need to use either [`playwright`](https://npmjs.com/package/playwright) or [`webdriverio`](https://www.npmjs.com/package/webdriverio) providers to enable this feature.
177+
:::
178+
122179
## Context
123180

124181
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.

0 commit comments

Comments
 (0)