Browser Mode: tests with the same name in different files fail with "tracing.stopChunk: file data stream has unexpected number of bytes"

Open Beginner friendly
#11,941 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
playwright, typescript
Domain
testing-qa

Research direction

Start in packages/browser/src/client/tester/runner.ts and read getTraceName, then check how the trace chunk name is used when parallel tests record traces. The report proposes adding the test id and limiting the sanitized title length; compare that approach with the failure screenshot naming issue and #11880. Done when same-named tests in different files get distinct trace files and long names stay within filesystem limits.

Written by the indexing model from the issue text.

Description

Describe the bug

With browser.trace set to retain-on-failure (or any mode that records), a passing test is reported as failed with:

FAIL  |chromium| src/components/Foo.stories.tsx > Default
Error: tracing.stopChunk: file data stream has unexpected number of bytes

It happens when tests with the same name in different test files run at the same time.
Storybook portable stories trigger it often, because Default and base are the usual story names.

getTraceName (packages/browser/src/client/tester/runner.ts) builds the chunk name only from the test name, the repeat count and the retry count.
The name does not include anything that identifies the test file or the test.
Playwright writes the files of a trace chunk under that name in a directory shared by all the browser contexts of the browser.
When two contexts record a chunk with the same name at once, they write the same files, and stopChunk fails when it zips them.
The failing test is the one that stops its chunk second, and it is reported as failed even though its assertions passed.

Evidence that the name is the cause:

  • We see it intermittently in CI and locally in Storybook test runs that run several story files in one browser (Vitest 5.1.0, @vitest/browser-playwright 5.1.0, Chromium).
    In the two logs we inspected, the failed tests were stories named Default, playground and base, and a story with the same name ran in another file of the same run.
  • pdevito3/FullstackTemplate#378 describes the same error for two same-named tests in two files, failing in 9 of 10 runs, and passing in 10 of 10 when the titles differ.
    pdevito3/FullstackTemplate#375 reports the same error in CI.

The related #11880 reports that the same name is also too long when the test name is long (ENAMETOOLONG).

Suggested fix

Make the chunk name unique per test and bounded in length: cut the sanitized title and add the test id.

function getTraceName(task: Task, retryCount: number, repeatsCount: number) {
  const title = getTestName(task, '-').replace(/[^a-z0-9]/gi, '-').slice(0, 100)
  return `${title}-${task.id.replace(/[^a-z0-9]/gi, '-')}-${repeatsCount}-${retryCount}`
}

The test id is a hash of the file path and project name plus the position of the test, so it is the same on every run.
A test that fails again overwrites its own trace file, and tests with the same title in different files no longer share one.
This also fixes the ENAMETOOLONG case from #11880 for traces.
Failure screenshots use getFailureScreenshotName, which has the same length problem.
We run this change as a patch and will open a PR if this direction is fine.

Reproduction

Two test files that each contain a test with the same name, run in one browser with browser.trace: 'retain-on-failure' and file parallelism on.
I do not have a standalone repository yet.
The linked FullstackTemplate issues describe one.

System Info
vitest: 5.1.0
@vitest/browser-playwright: 5.1.0
playwright: 1.56.1 (Chromium, headless)
Node: 22.20.0
OS: Linux (remote build workers)
Used Package Manager

pnpm

Dominant language
TypeScript
Stars
17.4k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
148

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vitest-dev/vitest

All issues in vitest-dev/vitest

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.