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/guide/browser.md
+92-35
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,93 @@ title: Browser Mode | Guide
6
6
7
7
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.
8
8
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
+
exportdefaultdefineConfig({
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
+
9
96
## Browser Compatibility
10
97
11
98
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.
43
130
44
131
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.
45
132
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
-
exportdefaultdefineConfig({
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
-
75
133
## Cross-Browser Testing
76
134
77
135
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:
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
-
97
149
## Headless
98
150
99
151
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:
In this case, Vitest will run in headless mode using the Chrome browser.
121
174
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
+
122
179
## Context
123
180
124
181
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