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/assertion-api.md
+32
Original file line number
Diff line number
Diff line change
@@ -63,3 +63,35 @@ If you are using TypeScript or want to have correct type hints in `expect`, make
63
63
}
64
64
```
65
65
:::
66
+
67
+
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:
68
+
69
+
```ts
70
+
import { expect, test } from'vitest'
71
+
import { screen } from'@testing-library/dom'
72
+
73
+
test('error banner is rendered', async () => {
74
+
triggerError()
75
+
76
+
// @testing-library provides queries with built-in retry-ability
77
+
// It will try to find the banner until it's rendered
78
+
const banner =awaitscreen.findByRole('alert', {
79
+
name:/error/i,
80
+
})
81
+
82
+
// Vitest provides `expect.element` with built-in retry-ability
83
+
// It will check `element.textContent` until it's equal to "Error!"
`expect.element` is a shorthand for `expect.poll(() => element)` and works in exactly the same way.
90
+
91
+
`toHaveTextContent` and all other [`@testing-library/jest-dom`](https://github.com/testing-library/jest-dom) assertions are still available on a regular `expect` without a built-in retry-ability mechanism:
92
+
93
+
```ts
94
+
// will fail immediately if .textContent is not `'Error!'`
Copy file name to clipboardexpand all lines: docs/guide/browser/context.md
+69-29
Original file line number
Diff line number
Diff line change
@@ -6,32 +6,13 @@ title: Context | Browser Mode
6
6
7
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.
8
8
9
-
```ts
10
-
exportconst server: {
11
-
/**
12
-
* Platform the Vitest server is running on.
13
-
* The same as calling `process.platform` on the server.
14
-
*/
15
-
platform:Platform
16
-
/**
17
-
* Runtime version of the Vitest server.
18
-
* The same as calling `process.version` on the server.
19
-
*/
20
-
version:string
21
-
/**
22
-
* Name of the browser provider.
23
-
*/
24
-
provider:string
25
-
/**
26
-
* Name of the current browser.
27
-
*/
28
-
browser:string
29
-
/**
30
-
* Available commands for the browser.
31
-
*/
32
-
commands:BrowserCommands
33
-
}
9
+
## `userEvent`
34
10
11
+
::: tip
12
+
The `userEvent` API is explained in detail at [Interactivity API](/guide/browser/interactivity-api).
13
+
:::
14
+
15
+
```ts
35
16
/**
36
17
* Handler for user interactions. The support is implemented by the browser provider (`playwright` or `webdriverio`).
37
18
* If used with `preview` provider, fallbacks to simulated events via `@testing-library/user-event`.
Commands API is explained in detail at [Commands](/guide/browser/commands).
46
+
:::
59
47
48
+
```ts
60
49
/**
61
50
* Available commands for the browser.
62
51
* A shortcut to `server.commands`.
63
52
*/
64
53
exportconst commands:BrowserCommands
54
+
```
55
+
56
+
## `page`
57
+
58
+
The `page` export provides utilities to interact with the current `page`.
59
+
60
+
::: warning
61
+
While it exposes some utilities from Playwright's `page`, it is not the same object. Since the browser context is evaluated in the browser, your tests don't have access to Playwright's `page` because it runs on the server.
The `cdp` export returns the current Chrome DevTools Protocol session. It is mostly useful to library authors to build tools on top of it.
85
+
86
+
::: warning
87
+
CDP session works only with `playwright` provider and only when using `chromium` browser. You can read more about it in playwright's [`CDPSession`](https://playwright.dev/docs/api/class-cdpsession) documentation.
88
+
:::
85
89
90
+
```ts
86
91
exportconst cdp: () =>CDPSession
87
92
```
93
+
94
+
## `server`
95
+
96
+
The `server` export represents the Node.js environment where the Vitest server is running. It is mostly useful for debugging.
97
+
98
+
```ts
99
+
exportconst server: {
100
+
/**
101
+
* Platform the Vitest server is running on.
102
+
* The same as calling `process.platform` on the server.
103
+
*/
104
+
platform:Platform
105
+
/**
106
+
* Runtime version of the Vitest server.
107
+
* The same as calling `process.version` on the server.
Copy file name to clipboardexpand all lines: docs/guide/browser/index.md
+6
Original file line number
Diff line number
Diff line change
@@ -47,10 +47,14 @@ bun add -D vitest @vitest/browser
47
47
48
48
::: warning
49
49
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 `preview` provider since it relies on simulating events instead of using Chrome DevTools Protocol.
50
+
51
+
If you don't already use one of these tools, we recommend starting with Playwright because it supports parallel execution, which makes your tests run faster. Additionally, the Chrome DevTools Protocol that Playwright uses is generally faster than WebDriver.
50
52
:::
51
53
52
54
### Using Playwright
53
55
56
+
[Playwright](https://npmjs.com/package/playwright) is a framework for Web Testing and Automation.
57
+
54
58
::: code-group
55
59
```bash [npm]
56
60
npm install -D vitest @vitest/browser playwright
@@ -68,6 +72,8 @@ bun add -D vitest @vitest/browser playwright
68
72
69
73
### Using Webdriverio
70
74
75
+
[WebdriverIO](https://www.npmjs.com/package/webdriverio) allows you to run tests locally using the WebDriver protocol.
0 commit comments