Skip to content

Commit af2b813

Browse files
authoredJul 31, 2024··
feat(browser): move page.config to server.config, add more docs (#6252)
1 parent 1da6ceb commit af2b813

File tree

9 files changed

+122
-88
lines changed

9 files changed

+122
-88
lines changed
 

‎docs/.vitepress/config.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,6 @@ export default ({ mode }: { mode: string }) => {
257257
link: '/guide/browser/',
258258
collapsed: false,
259259
items: [
260-
{
261-
text: 'Assertion API',
262-
link: '/guide/browser/assertion-api',
263-
docFooterText: 'Assertion API | Browser Mode',
264-
},
265-
{
266-
text: 'Retry-ability',
267-
link: '/guide/browser/retry-ability',
268-
docFooterText: 'Retry-ability | Browser Mode',
269-
},
270260
{
271261
text: 'Context',
272262
link: '/guide/browser/context',
@@ -277,6 +267,11 @@ export default ({ mode }: { mode: string }) => {
277267
link: '/guide/browser/interactivity-api',
278268
docFooterText: 'Interactivity API | Browser Mode',
279269
},
270+
{
271+
text: 'Assertion API',
272+
link: '/guide/browser/assertion-api',
273+
docFooterText: 'Assertion API | Browser Mode',
274+
},
280275
{
281276
text: 'Commands',
282277
link: '/guide/browser/commands',

‎docs/guide/browser/assertion-api.md

+32
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,35 @@ If you are using TypeScript or want to have correct type hints in `expect`, make
6363
}
6464
```
6565
:::
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 = await screen.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!"
84+
await expect.element(banner).toHaveTextContent('Error!')
85+
})
86+
```
87+
88+
::: tip
89+
`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!'`
95+
expect(banner).toHaveTextContent('Error!')
96+
```
97+
:::

‎docs/guide/browser/context.md

+69-29
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,13 @@ title: Context | Browser Mode
66

77
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.
88

9-
```ts
10-
export const 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`
3410

11+
::: tip
12+
The `userEvent` API is explained in detail at [Interactivity API](/guide/browser/interactivity-api).
13+
:::
14+
15+
```ts
3516
/**
3617
* Handler for user interactions. The support is implemented by the browser provider (`playwright` or `webdriverio`).
3718
* If used with `preview` provider, fallbacks to simulated events via `@testing-library/user-event`.
@@ -56,18 +37,32 @@ export const userEvent: {
5637
fill: (element: Element, text: string, options?: UserEventFillOptions) => Promise<void>
5738
dragAndDrop: (source: Element, target: Element, options?: UserEventDragAndDropOptions) => Promise<void>
5839
}
40+
```
41+
42+
## `commands`
43+
44+
::: tip
45+
Commands API is explained in detail at [Commands](/guide/browser/commands).
46+
:::
5947
48+
```ts
6049
/**
6150
* Available commands for the browser.
6251
* A shortcut to `server.commands`.
6352
*/
6453
export const 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.
62+
:::
6563
64+
```ts
6665
export const page: {
67-
/**
68-
* Serialized test config.
69-
*/
70-
config: SerializedConfig
7166
/**
7267
* Change the size of iframe's viewport.
7368
*/
@@ -82,6 +77,51 @@ export const page: {
8277
}>
8378
screenshot(options?: ScreenshotOptions): Promise<string>
8479
}
80+
```
81+
82+
## `cdp`
83+
84+
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+
:::
8589
90+
```ts
8691
export const cdp: () => CDPSession
8792
```
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+
export const 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.
108+
*/
109+
version: string
110+
/**
111+
* Name of the browser provider.
112+
*/
113+
provider: string
114+
/**
115+
* Name of the current browser.
116+
*/
117+
browser: string
118+
/**
119+
* Available commands for the browser.
120+
*/
121+
commands: BrowserCommands
122+
/**
123+
* Serialized test config.
124+
*/
125+
config: SerializedConfig
126+
}
127+
```

‎docs/guide/browser/index.md

+6
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ bun add -D vitest @vitest/browser
4747

4848
::: warning
4949
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.
5052
:::
5153

5254
### Using Playwright
5355

56+
[Playwright](https://npmjs.com/package/playwright) is a framework for Web Testing and Automation.
57+
5458
::: code-group
5559
```bash [npm]
5660
npm install -D vitest @vitest/browser playwright
@@ -68,6 +72,8 @@ bun add -D vitest @vitest/browser playwright
6872

6973
### Using Webdriverio
7074

75+
[WebdriverIO](https://www.npmjs.com/package/webdriverio) allows you to run tests locally using the WebDriver protocol.
76+
7177
::: code-group
7278
```bash [npm]
7379
npm install -D vitest @vitest/browser webdriverio

‎docs/guide/browser/retry-ability.md

-37
This file was deleted.

‎packages/browser/context.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ export const server: {
233233
* @see {@link https://vitest.dev/guide/browser/commands}
234234
*/
235235
commands: BrowserCommands
236+
/**
237+
* Serialized test config.
238+
*/
239+
config: SerializedConfig
236240
}
237241

238242
/**
@@ -250,10 +254,6 @@ export const userEvent: UserEvent
250254
export const commands: BrowserCommands
251255

252256
export interface BrowserPage {
253-
/**
254-
* Serialized test config.
255-
*/
256-
config: SerializedConfig
257257
/**
258258
* Change the size of iframe's viewport.
259259
*/

‎packages/browser/src/client/tester/context.ts

-3
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,6 @@ export function cdp() {
246246

247247
const screenshotIds: Record<string, Record<string, string>> = {}
248248
export const page: BrowserPage = {
249-
get config() {
250-
return runner().config
251-
},
252249
viewport(width, height) {
253250
const id = runner().iframeId
254251
channel.postMessage({ type: 'viewport', width, height, id })

‎packages/browser/src/node/plugins/pluginContext.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ export const server = {
8080
browser: ${JSON.stringify(server.project.config.browser.name)},
8181
commands: {
8282
${commandsCode}
83-
}
83+
},
84+
config: __vitest_browser_runner__.config,
8485
}
8586
export const commands = server.commands
8687
export const userEvent = ${getUserEvent(provider)}

‎test/browser/test/viewport.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { page, server } from '@vitest/browser/context'
1+
import { server } from '@vitest/browser/context'
22
import { describe, expect, it } from 'vitest'
33

44
describe.skipIf(server.provider === 'preview')('viewport window has been properly initialized', () => {
5-
it.skipIf(!page.config.browser.headless)('viewport has proper size', () => {
6-
const { width, height } = page.config.browser.viewport
5+
it.skipIf(!server.config.browser.headless)('viewport has proper size', () => {
6+
const { width, height } = server.config.browser.viewport
77
const { width: actualWidth, height: actualHeight } = window.document.documentElement.getBoundingClientRect()
88

99
expect(actualWidth).toBe(width)
1010
expect(actualHeight).toBe(height)
1111
})
1212

13-
it.skipIf(page.config.browser.headless)('window has been maximized', () => {
13+
it.skipIf(server.config.browser.headless)('window has been maximized', () => {
1414
let topWindow = window
1515
while (topWindow.parent && topWindow !== topWindow.parent) {
1616
topWindow = topWindow.parent as unknown as any

0 commit comments

Comments
 (0)
Please sign in to comment.