Browser mode: failure screenshots fail with ENAMETOOLONG on long test names
Clear and concise description of the problem
With browser.screenshotFailures on, the failure screenshot is named after the test's full name: page.screenshot() is called without a path, so the name becomes getTaskFullName(task).replace(/[^a-z0-9]/gi, "-") + "-<n>.png" (@vitest/browser dist/context.js; unchanged in 4.1.11 and 5.0.3).
BDD adapters such as @amiceli/vitest-cucumber nest a test as Feature: ... > Scenario: ... > <step>, so a full name easily runs past the 255-byte file-name limit (APFS, ext4). The capture then fails, and the screenshot is lost:
[vitest] Failed to take a screenshot [Error: locator.screenshot: ENAMETOOLONG: name too long, open '.../__screenshots__/x.feature.rendered.test.ts/Feature--The-section-nav-pins-under-the-page-title-and-marks-the-section-in-view-Scenario--...-1.png']
In our suite one scenario's step names reach 607 characters. browser.screenshotDirectory moves the directory but cannot shorten the file name, so the only remedy available today is screenshotFailures: false.
Suggested solution
Any one of these would do:
- Truncate the sanitized name to fit the file-name limit, and append a short hash of the full name so names stay unique.
- Accept a
resolveFailureScreenshotPathoption, likebrowser.expect.toMatchScreenshot.resolveScreenshotPath.
Alternative
Turn off screenshotFailures for projects with long test names.
Additional context
Reproduces with Vitest 4.1.11 and @vitest/browser-playwright, on Chromium and WebKit.
Validations
- [x] Follow our Code of Conduct
- [x] Read the Contributing Guidelines.
- [x] Check that there isn't already an issue that requests the same feature to avoid creating a duplicate.
Looks similar to https://github.com/vitest-dev/vitest/issues/9006. The file name generation logic is actually different but both use test title so it can get long easily.
Quick triage note 🤖 https://gist.github.com/hi-ogawa/de844e7b91ba25971589b43288a6797a#where-file-names-are-built
I'd like to fix this together with #9006, following the triage note. I have a working branch: diff.
- A new helper,
limitFileName(name, suffix), sits next tosanitizeFilePathin@vitest/utils/helpers. It caps the file name at 255 bytes, counting UTF-8 bytes sincesanitizeFilePathkeeps non-ASCII characters. If the name has to be shortened, it appends a short hash of the full name so names stay unique. Names that already fit don't change. - It's used for the failure screenshot, the default
page.screenshot()name, the trace zip, and the attachment copy inresolveTestAttachment. - Shortening the trace path wasn't enough. Playwright also names its own temporary files after the
startChunkname and adds suffixes like-pwnetcopy-1.network, so long names still failed there. The name is optional and only used for those temporary files, so the branch stops passing it. - Windows
MAX_PATHis out of scope, because capping the file name can't fix the full path length.
The tests use a 300-character test name for both failure screenshots and traces. They fail without the change and pass with it on Chromium, Firefox, and WebKit.
Does this direction work for you? I can open a PR if you'd like one.
Small follow-up: I rebased the branch onto current main. It now works together with the new retry/repeat screenshot suffixes. Would a PR be welcome, or would you rather take a different approach?
Happens to us too, 1900 failures with names up to 500 chars, @nomadical fix seem to catch 2 birds with one stone, it would be ideal.
@hi-ogawa francesco-agnoletto's team is hitting this too (about 1,900 failures with long test names), and it covers #9006 as well. The fork branch still merges cleanly into main. Could you take a look when you have time and let me know whether a PR is welcome?
Opened #11848 for the trace part of #9006 following the approach in the issue (basename, no trace-zip).
It doesn't cap names, so the helper here is still needed for screenshots and very long titles.
Thanks @francesco-agnoletto for #11848. It covers the trace annotation for #9006, so I'll leave that part to you.
My branch still covers the rest: 79c1232f5. It adds limitFileName(name, suffix) to @vitest/utils/helpers, which caps a file name at 255 bytes and replaces the cut part with a short hash. It's used for:
- failure screenshots (
runner.ts) - the default
page.screenshot()name (context.ts) - attachment paths in
resolveTestAttachment(test-run.ts) - the trace zip path, and
startChunkno longer getsname, because Playwright adds its own suffixes to temp files
It merges cleanly into main. With #11848 the only conflict is an import line in trace.ts, so I'd rebase once that lands.
Happy to open a PR once #11848 is in.