Repository navigation
Description
- introduced by feat(browser): support dom snapshot trace view #10102
- introduced by feat: add
page/locator.markAPI to enhance playwright trace #9652 - related Trace View: Failure-Only Trace Recording #10310 (
'failure-only'mode, where the test-end entry holds the only snapshot)
Describe the bug
Both trace features record the end of each test attempt in onAfterRetryTask, which runs after afterEach hooks and cleanups. When a test cleans up the page in afterEach, the end of the trace shows an empty page instead of the page the test ended on.
browser.traceView: thevitest:onAfterRetryTaskentry, shown as "test finished" in the trace viewer, takes its DOM snapshot there.browser.trace(Playwright): theonAfterRetryTask [state]marker is emitted there, andmarkTraceforces a Playwright snapshot for every marker (group plus a dummypage.evaluate).
The failure screenshot doesn't have this problem, because it is taken in onTaskFinished, which runs after the test body and before afterEach.
vitest-browser-react and vitest-browser-vue clean up in beforeEach, so their users aren't affected. It shows up with cleanup in afterEach, which is the pattern in the setup file examples of the docs (config/setupfiles.md, guide/lifecycle.md).
#8584 used onAfterRetryTask to stop each attempt's Playwright trace chunk, which is fine there because stopping a chunk captures nothing. #9652 added the snapshotting marker to the same hook, and #10102 followed it for trace view. The hook is still right for the trace view entry's status and end time, since a failure thrown in afterEach is only known there. Only the snapshots need to be taken earlier, at the same time as the failure screenshot.
This matters more with #10310's 'failure-only' mode, where the test-end entry holds the only snapshot of a failed test, and with #11492, which adds an aria snapshot to the same entry.
Reproduction
import { afterEach, expect, test } from 'vitest'
import { page } from 'vitest/browser'
afterEach(() => {
document.body.innerHTML = ''
})
test('fail', async () => {
document.body.innerHTML = '<button>Marker Content</button>'
await page.getByRole('button').click()
expect(1).toBe(2)
})Run with browser.traceView: true and open the trace. The click step shows the button, and the "test finished" step shows an empty page.
Checked through the recorded artifacts: the vitest:click entries' snapshots contain Marker Content, and the vitest:onAfterRetryTask entry's snapshot doesn't.
With browser.trace: 'on' instead, the onAfterRetryTask [fail] marker in the Playwright trace is expected to show the empty page as well, since it snapshots at the same point. This one follows from the code and hasn't been run.
Suggested fix
Take both end-of-attempt snapshots in onTaskFinished, where the failure screenshot is taken.
browser.traceView: take the snapshot inonTaskFinishedand attach it to thevitest:onAfterRetryTaskentry when that entry is recorded. The entry keeps its status, location, and end time fromonAfterRetryTask.browser.trace: a Playwright marker snapshots when it is emitted, so the marker itself moves toonTaskFinished. Its state then doesn't include a failure thrown inafterEach. The chunk stop stays inonAfterRetryTask.
System Info
Vitest main (b9cd11d on feat/issue-11592), Playwright provider, chromium.
Activity
hi-ogawa commentedon Oct 21, 2026
Closing as par #11654 (comment)