Skip to content

Commit 1aac59c

Browse files
authored
feat(browser): support playwright tracing (#8584)
1 parent dd4bbb7 commit 1aac59c

File tree

28 files changed

+771
-33
lines changed

28 files changed

+771
-33
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ docs/public/sponsors
2525
docs/.vitepress/cache/
2626
!test/cli/fixtures/dotted-files/**/.cache
2727
test/**/__screenshots__/**/*
28+
test/**/__traces__/**/*
2829
test/browser/fixtures/update-snapshot/basic.test.ts
2930
test/cli/fixtures/browser-multiple/basic-*
3031
.vitest-reports

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// ],
2424

2525
"vitest.ignoreWorkspace": true,
26-
"vitest.configSearchPatternInclude": "test/{core,cli,config}/{vitest,vite}.config.ts",
26+
"vitest.configSearchPatternInclude": "test/{core,cli,config,browser}/{vitest,vite}.{config.ts,config.unit.mts}",
2727
"testing.automaticallyOpenTestResults": "neverOpen",
2828

2929
// Enable eslint for all supported languages

docs/.vitepress/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,11 @@ export default ({ mode }: { mode: string }) => {
305305
link: '/guide/browser/visual-regression-testing',
306306
docFooterText: 'Visual Regression Testing | Browser Mode',
307307
},
308+
{
309+
text: 'Trace Viewer',
310+
link: '/guide/browser/trace-viewer',
311+
docFooterText: 'Trace Viewer | Browser Mode',
312+
},
308313
],
309314
},
310315
{

docs/advanced/runner.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,24 @@ export interface VitestRunner {
2727
/**
2828
* Called before running a single test. Doesn't have "result" yet.
2929
*/
30-
onBeforeRunTask?: (test: TaskPopulated) => unknown
30+
onBeforeRunTask?: (test: Test) => unknown
3131
/**
3232
* Called before actually running the test function. Already has "result" with "state" and "startTime".
3333
*/
34-
onBeforeTryTask?: (test: TaskPopulated, options: { retry: number; repeats: number }) => unknown
34+
onBeforeTryTask?: (test: Test, options: { retry: number; repeats: number }) => unknown
3535
/**
3636
* Called after result and state are set.
3737
*/
38-
onAfterRunTask?: (test: TaskPopulated) => unknown
38+
onAfterRunTask?: (test: Test) => unknown
3939
/**
4040
* Called right after running the test function. Doesn't have new state yet. Will not be called, if the test function throws.
4141
*/
42-
onAfterTryTask?: (test: TaskPopulated, options: { retry: number; repeats: number }) => unknown
42+
onAfterTryTask?: (test: Test, options: { retry: number; repeats: number }) => unknown
43+
/**
44+
* Called after the retry resolution happend. Unlike `onAfterTryTask`, the test now has a new state.
45+
* All `after` hooks were also called by this point.
46+
*/
47+
onAfterRetryTask?: (test: Test, options: { retry: number; repeats: number }) => unknown
4348

4449
/**
4550
* Called before running a single suite. Doesn't have "result" yet.

docs/config/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2439,7 +2439,7 @@ Polling timeout in milliseconds
24392439

24402440
Always print console traces when calling any `console` method. This is useful for debugging.
24412441

2442-
### attachmentsDir <Version>3.2.0</Version>
2442+
### attachmentsDir <Version>3.2.0</Version> {#attachmentsdir}
24432443

24442444
- **Type:** `string`
24452445
- **Default:** `'.vitest-attachments'`

docs/guide/browser/config.md

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ A path to the HTML entry point. Can be relative to the root of the project. This
128128

129129
Configure options for Vite server that serves code in the browser. Does not affect [`test.api`](#api) option. By default, Vitest assigns port `63315` to avoid conflicts with the development server, allowing you to run both in parallel.
130130

131-
## browser.provider <Badge type="danger">advanced</Badge> {#browser-provider}
131+
## browser.provider {#browser-provider}
132132

133133
- **Type:** `BrowserProviderOption`
134134
- **Default:** `'preview'`
@@ -154,7 +154,7 @@ export default defineConfig({
154154

155155
To configure how provider initializes the browser, you can pass down options to the factory function:
156156

157-
```ts{7-15,22-27}
157+
```ts{7-13,20-26}
158158
import { playwright } from '@vitest/browser/providers/playwright'
159159
160160
export default defineConfig({
@@ -188,7 +188,7 @@ export default defineConfig({
188188
})
189189
```
190190

191-
### Custom Provider
191+
### Custom Provider <Badge type="danger">advanced</Badge>
192192

193193
::: danger ADVANCED API
194194
The custom provider API is highly experimental and can change between patches. If you just need to run tests in a browser, use the [`browser.instances`](#browser-instances) option instead.
@@ -307,6 +307,50 @@ The timeout in milliseconds. If connection to the browser takes longer, the test
307307
This is the time it should take for the browser to establish the WebSocket connection with the Vitest server. In normal circumstances, this timeout should never be reached.
308308
:::
309309

310+
## browser.trace
311+
312+
- **Type:** `'on' | 'off' | 'on-first-retry' | 'on-all-retries' | 'retain-on-failure' | object`
313+
- **CLI:** `--browser.trace=on`, `--browser.trace=retain-on-failure`
314+
- **Default:** `'off'`
315+
316+
Capture a trace of your browser test runs. You can preview traces with [Playwright Trace Viewer](https://trace.playwright.dev/).
317+
318+
This options supports the following values:
319+
320+
- `'on'` - capture trace for all tests. (not recommended as it's performance heavy)
321+
- `'off'` - do not capture traces.
322+
- `'on-first-retry'` - capture trace only when retrying the test for the first time.
323+
- `'on-all-retries'` - capture trace on every retry of the test.
324+
- `'retain-on-failure'` - capture trace only for tests that fail. This will automatically delete traces for tests that pass.
325+
- `object` - an object with the following shape:
326+
327+
```ts
328+
interface TraceOptions {
329+
mode: 'on' | 'off' | 'on-first-retry' | 'on-all-retries' | 'retain-on-failure'
330+
/**
331+
* The directory where all traces will be stored. By default, Vitest
332+
* stores all traces in `__traces__` folder close to the test file.
333+
*/
334+
tracesDir?: string
335+
/**
336+
* Whether to capture screenshots during tracing. Screenshots are used to build a timeline preview.
337+
* @default true
338+
*/
339+
screenshots?: boolean
340+
/**
341+
* If this option is true tracing will
342+
* - capture DOM snapshot on every action
343+
* - record network activity
344+
* @default true
345+
*/
346+
snapshots?: boolean
347+
}
348+
```
349+
350+
::: danger WARNING
351+
This option is supported only by the [**playwright**](/guide/browser/playwright) provider.
352+
:::
353+
310354
## browser.trackUnhandledErrors
311355

312356
- **Type:** `boolean`

docs/guide/browser/playwright.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default defineConfig({
1818

1919
Vitest opens a single page to run all tests in the same file. You can configure the `launch`, `connect` and `context` when calling `playwright` at the top level or inside instances:
2020

21-
```ts{7-15,22-27} [vitest.config.js]
21+
```ts{7-14,21-26} [vitest.config.js]
2222
import { playwright } from '@vitest/browser/providers/playwright'
2323
import { defineConfig } from 'vitest/config'
2424

docs/guide/browser/trace-viewer.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Trace Viewer
2+
3+
Vitest Browser Mode supports generating Playwright's [trace files](https://playwright.dev/docs/trace-viewer#viewing-remote-traces). To enable tracing, you need to set the [`trace`](/guide/browser/config#browser-trace) option in the `test.browser` configuration.
4+
5+
::: warning
6+
Generating trace files is only available when using the [Playwright provider](/guide/browser/playwright).
7+
:::
8+
9+
::: code-group
10+
```ts [vitest.config.js]
11+
import { defineConfig } from 'vitest/config'
12+
import { playwright } from '@vitest/browser/providers/playwright'
13+
14+
export default defineConfig({
15+
test: {
16+
browser: {
17+
provider: playwright(),
18+
trace: 'on',
19+
},
20+
},
21+
})
22+
```
23+
```bash [CLI]
24+
vitest --browser.trace=on
25+
```
26+
:::
27+
28+
By default, Vitest will generate a trace file for each test. You can also configure it to only generate traces on test failures by setting `trace` to `'on-first-retry'`, `'on-all-retries'` or `'retain-on-failure'`. The files will be saved in `__traces__` folder next to your test files. The name of the trace includes the project name, the test name, the [`repeats` count and `retry` count](/api/#test-api-reference):
29+
30+
```
31+
chromium-my-test-0-0.trace.zip
32+
^^^^^^^^ project name
33+
^^^^^^ test name
34+
^ repeat count
35+
^ retry count
36+
```
37+
38+
To change the output directory, you can set the `tracesDir` option in the `test.browser.trace` configuration. This way all traces will be stored in the same directory, grouped by the test file.
39+
40+
```ts [vitest.config.js]
41+
import { defineConfig } from 'vitest/config'
42+
import { playwright } from '@vitest/browser/providers/playwright'
43+
44+
export default defineConfig({
45+
test: {
46+
browser: {
47+
provider: playwright(),
48+
trace: {
49+
mode: 'on',
50+
// the path is relative to the root of the project
51+
tracesDir: './playwright-traces',
52+
},
53+
},
54+
},
55+
})
56+
```
57+
58+
The traces are available in reporters as [annotations](/guide/test-annotations). For example, in the HTML reporter, you can find the link to the trace file in the test details.
59+
60+
## Preview
61+
62+
To open the trace file, you can use the Playwright Trace Viewer. Run the following command in your terminal:
63+
64+
```bash
65+
npx playwright show-trace "path-to-trace-file"
66+
```
67+
68+
This will start the Trace Viewer and load the specified trace file.
69+
70+
Alternatively, you can open the Trace Viewer in your browser at https://trace.playwright.dev and upload the trace file there.
71+
72+
## Limitations
73+
74+
At the moment, Vitest cannot populate the "Sources" tab in the Trace Viewer. This means that while you can see the actions and screenshots captured during the test, you won't be able to view the source code of your tests directly within the Trace Viewer. You will need to refer back to your code editor to see the test implementation.

docs/guide/cli-generated.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,13 @@ If connection to the browser takes longer, the test suite will fail (default: `6
376376

377377
Control if Vitest catches uncaught exceptions so they can be reported (default: `true`)
378378

379+
### browser.trace
380+
381+
- **CLI:** `--browser.trace <mode>`
382+
- **Config:** [browser.trace](/guide/browser/config#browser-trace)
383+
384+
Enable trace view mode. Supported: "on", "off", "on-first-retry", "on-all-retries", "retain-on-failure".
385+
379386
### pool
380387

381388
- **CLI:** `--pool <pool>`

0 commit comments

Comments
 (0)