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

feature: use whole snapshot file to store data, do not store data as string #27

Closed
milahu opened this issue Dec 22, 2020 · 7 comments
Closed

Comments

@milahu
Copy link

milahu commented Dec 22, 2020

expected result (content of snapshot file)

snapshot data

no need to `escape` "anything \"in\" here"

actual result

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`test name 1`] = `
snapshot data

unnecessary \`escapes\` "all \\"over\\" the place"
`;

challenges

  • replace function saveSnapshotFile called by jest-snapshot/State.save()
  • fix the reading of snapshot files
  • generate file paths from test names
    use test-filename as directory name, and store test snapshots as files in that dir

use case
in my tests i record API calls to a html canvas elemet (using jest-canvas-mock)
and im storing the events as javascript source code
benefits:
snapshot is human-readable and machine-readable
recorded events can easily be replayed to a live canvas element

@Maxim-Mazurok
Copy link

Maxim-Mazurok commented Sep 22, 2022

Same for our use-case, we generate TS type definitions, markdown, etc. and would like to have them as separate files with syntax highlight, see Maxim-Mazurok/google-api-typings-generator#706

@igor-dv
Copy link
Owner

igor-dv commented Mar 21, 2023

With a short delay 😅 @Maxim-Mazurok , @milahu
Is it something that still needed ? Jest already do not escape characters, so the result is a bit better, but still having a few results per file was the initial meaning of this matcher. But it maybe can be done as a separate matcher.

@Maxim-Mazurok
Copy link

Hi Igor :) Yes, having them as separate files would be very nice to have! We're still using this library and have a 2MB snapshot with all the files generated for testing... https://github.com/Maxim-Mazurok/google-api-typings-generator/blob/master/test/restDocs/__snapshots__/test.spec.ts.snap

This likely goes against the idea of reviewing snapshots like a normal code... But our goal was to use snapshots to ensure that we don't break generation when refactoring code, so I think they worked very well for our case.

This is however relatively low priority at the moment as these weren't touched in a while, but I have some ideas to migrate from generating markdown with strings and use some sort of programmatic generation, which will be huge change and we'll need snapshots again when working on Maxim-Mazurok/google-api-typings-generator#657

@milahu
Copy link
Author

milahu commented Mar 25, 2023

update: i implemented a proof-of-concept of such a "one file per snapshot" test runner with jest-file-snapshot in milahu/mitosis@69382fd (see also BuilderIO/mitosis#856 (comment))

@Maxim-Mazurok
Copy link

Interesting, sounds like jest-file-snapshot does exactly what I wanted, I'll give it a shot, thanks for update!

@igor-dv
Copy link
Owner

igor-dv commented Apr 11, 2023

Cool, going to close this one then.

@igor-dv igor-dv closed this as completed Apr 11, 2023
@Maxim-Mazurok
Copy link

Randomly found out that vitest has .toMatchFileSnapshot() which seems like what we wanted to achieve here, see https://vitest.dev/guide/snapshot.html#file-snapshots

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants