Skip to content

Add options to resolveSnapshotPath - #1620

Closed
hermanbanken wants to merge 1 commit into
vitest-dev:mainfrom
hermanbanken:feature/resolve-snapshot-path-options
Closed

hermanbanken wants to merge 1 commit into
vitest-dev:mainfrom
hermanbanken:feature/resolve-snapshot-path-options

Conversation

@hermanbanken

Copy link
Copy Markdown

My team wants to save our snapshots in different files, depending on some variables that are defined by a table (describe.each). Specifically we want to run the same test for various versions of clients. Storing all of these snapshots in the same file has some very annoying downsides, because the diff tools will confuse the addition and deletion of versions and display generally unhelpful outputs for human review.

Today the snapshot path depends only on the test file and the extension (.snap) and given that it runs in a different thread (using some RPC protocol) we can't pass down variables using AsyncLocalStorage or other mechanisms with global state.

This PR adds the easily customizable Test.Context object to the snapshot resolver, allowing users to conveniently specify a different path per context.

I'm not sure this works due to the nature of the RPC: can it send other parameters than strings? Does it fall apart if the object isn't JSON serialisable? I'm not sure. But I wanted to create this PR already, to make the proposal and gather some opinions.

@netlify

netlify Bot commented Jul 9, 2022 •

Copy link
Copy Markdown

โœ… Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
๐Ÿ”จ Latest commit d82aa87
๐Ÿ” Latest deploy log https://app.netlify.com/sites/vitest-dev/deploys/62c953cbacaa9200087cad5d
๐Ÿ˜Ž Deploy Preview https://deploy-preview-1620--vitest-dev.netlify.app
๐Ÿ“ฑ Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@sheremet-va

sheremet-va commented Jul 9, 2022 •

Copy link
Copy Markdown
Member

I'm not sure this works due to the nature of the RPC: can it send other parameters than strings? Does it fall apart if the object isn't JSON serialisable? I'm not sure. But I wanted to create this PR already, to make the proposal and gather some opinions.

These are the rules for serialisation: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm

If it fails to clone, it will throw an error.

Context is actually a function, so it cannot be cloned.

@sheremet-va

sheremet-va commented Jul 31, 2022 •

Copy link
Copy Markdown
Member

I am not against the idea, but we will need to change how we resolve snapshot paths. If we change resolveSnapshotPath from function to a string (path to a module), then we can import it inside test thread, and pass whatever we want. This will be a breaking change:

resolveSnapshotPath?: (path: string, extension: string) => string

- resolveSnapshotPath?: (path: string, extension: string) => string
+ resolveSnapshotPath?: string

The module should return default as a function that we used previously.

@thomasmattheussen

Copy link
Copy Markdown

My team wants to save our snapshots in different files

We have the exact same problem. We'd like to use vitest but are now stuck using jest with https://github.com/igor-dv/jest-specific-snapshot

@unional

unional commented Oct 21, 2024 •

Copy link
Copy Markdown

I'm using workspace and browser mode and here is my 2 cents:

  • to work with workspace, the approach also needs projectRoot. For string approach, that means some templating ({projectRoot}/... or <projectRoot>/...)
  • page.screenshot() creates images with counter (a.spec.tsx/test-something-{1..n}.png). resolveSnapshotPath() does not have a way to do the same. Need that counter or additional templating.

@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown

Hello @hermanbanken. Thank you for taking the time to contribute!

Unfortunately, the team accepts pull requests only from maintainers and approved contributors, so this pull request was closed automatically. We are sorry about that, it is not a judgement of your work. The number of pull requests grew beyond what the team can review, and this policy gives maintainers the space to triage and prioritize issues at their own pace.

If there is no issue for this change yet, please open one to discuss it with the team first. Your changes are not lost: a maintainer can reopen this pull request if the team decides to go forward with it. See our pull request policy for more context.

@github-actions github-actions Bot closed this Oct 5, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 21, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

breaking change discussion p2-nice-to-have Not breaking anything but nice to have (priority)

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants