Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add toMatchSpecificSnapshot #2341

Closed
4 tasks done
MichaelBitard opened this issue Nov 16, 2022 · 6 comments
Closed
4 tasks done

add toMatchSpecificSnapshot #2341

MichaelBitard opened this issue Nov 16, 2022 · 6 comments

Comments

@MichaelBitard
Copy link

Clear and concise description of the problem

We use storybook in our vue project, and we used jest and storyshots (automated snapshot testing)

We are migrating to vitest for multiple reasons, and we are trying to replace storyshots which works only with jest (storybookjs/storybook#17578)

After playing with vitest, we are able to generate on test for each storybook case, and snapshot the associated html.

Our only issue is that all our storybook are run by the same test file, and the toMatchSnapshot generate a lot of snapshots in the same file.

We would like to create a snapshot file by storybook file, and to do that we would need the jest equivalent toMatchSpecificSnapshot

Suggested solution

I don't really know how to implement it, I know the signature should be something like

toMatchSpecificSnapshot<T>(pathToSnapshotFile: string,shape?: Partial<T> | string, message?: string)

Alternative

We wanted to play with inlinesnapshot and programmatically write/retrieve the file, but we lose all the automatic update from vitest

Additional context

No response

Validations

@sheremet-va
Copy link
Member

Can't you use resolveSnapshotPath?

@MichaelBitard
Copy link
Author

That could be a good idea, but I think we don't have enough context to know which storybook is getting tested, since the test we wrote is a meta test, that load all the storybooks, render each story in a test, snapshot the rendered html

In almost pseudocode, our test does this:

describe('Storybook Smoke Test', async () => {
  // We load all the storybook files
  const modules = await Promise.all(
    Object.values(import.meta.glob<StoryFile>('../**/*.stories.ts')).map(fn =>
      fn()
    )
  )

  describe.each(modules)('%s', (module) => {
   // for each module, we create a test for each story
    test.each(Object.values(composeStories(module)))('%s', story) => {
      const mounted = render(story())
      expect(mounted.html()).toMatchSnapshot()
    })
  })
})

Am I missing something in resolveSnapshotPath that could tell us which test file/describe is being executed?

@sheremet-va
Copy link
Member

Am I missing something in resolveSnapshotPath that could tell us which test file/describe is being executed?

No, currently it doesn't know. There was a discussion to add context to it, which has this information in #1620 but it is not implemented and will require some refactoring and will be a breaking change (instead of function, users will need to provide a string path, like with setupFiles, which is fine, I think)

@MichaelBitard
Copy link
Author

Thanks for your answer!

If I wanted to try and implement it, I should look at similar stuff here right? #43

@sheremet-va
Copy link
Member

If I wanted to try and implement it, I should look at similar stuff here right? #43

No, I don't think it is related. If you want to implement it in Vitest, I would look into #1620. The only change is needed there is initiating resolveSnapshotPath inside a worker thread instead of using RPC.

@thomasmattheussen
Copy link

The newly added File Snapshots feature seems to resolve this 🙌

@github-actions github-actions bot locked and limited conversation to collaborators Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants