Skip to content

Test-end browser trace snapshots are taken after afterEach cleanup #11615

Closed

Description

@hi-ogawa

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: the vitest:onAfterRetryTask entry, shown as "test finished" in the trace viewer, takes its DOM snapshot there.
  • browser.trace (Playwright): the onAfterRetryTask [state] marker is emitted there, and markTrace forces a Playwright snapshot for every marker (group plus a dummy page.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 in onTaskFinished and attach it to the vitest:onAfterRetryTask entry when that entry is recorded. The entry keeps its status, location, and end time from onAfterRetryTask.
  • browser.trace: a Playwright marker snapshots when it is emitted, so the marker itself moves to onTaskFinished. Its state then doesn't include a failure thrown in afterEach. The chunk stop stays in onAfterRetryTask.

System Info

Vitest main (b9cd11d on feat/issue-11592), Playwright provider, chromium.

Activity

  1. added
    feat: browser
    p3-minor-bug
    on Oct 16, 2026
  2. added theissue type on Oct 16, 2026
  3. hi-ogawa commented on Oct 21, 2026

    @hi-ogawa
    CollaboratorAuthor

    Closing as par #11654 (comment)

  4. locked and limited conversation to collaborators on Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: browserp3-minor-bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions