Giter VIP home page Giter VIP logo

Comments (6)

hi-ogawa avatar hi-ogawa commented on August 16, 2024 1

Yeah, probably it's not actionable for Vitest for now. Nonetheless, thanks for raising an issue with a concise reproduction.

Btw, one more limitation of toMatchFileSnapshot. As explained in the following issue, it cannot automatically delete obsolete snapshot files.

If you have many files to manage, then you might need to write your own script or something to make sure "helm files" and "helm snapshot files" correspond to each other.

from vitest.

hi-ogawa avatar hi-ogawa commented on August 16, 2024

I think this is because the main use case is to just dump string directly to a file, so serialization is skipped for raw string input:

let receivedSerialized = (rawSnapshot && typeof received === 'string')
? received as string
: serialize(received, undefined, this._snapshotFormat)

Otherwise default string serializer would write quoted "hello" instead of hello to the file by default.

Actually if the input is not a raw string like expect({ hello: "world" }).toMatchFileSnapshot(), then serializers (including custom ones) will be applied.

If you happen to have a specific use case to apply custom serializer for raw string, could you share the motivation? Maybe we can suggest some okay-ish workaround.

I don't think there's a good way to apply just user's custom serializers, so probably we cannot solve this issue/limitation in a general manner.

from vitest.

dangmai avatar dangmai commented on August 16, 2024

My use case is that I'm using vitest to make sure that Kubernetes manifests generated by helm haven't changed when new versions are released. However, those manifests do contain the version numbers which are expected to change, and I want to use a custom serializer to swap out those version numbers for static strings (so that the tests don't fail). These are all raw strings since they're generated by a non-Node tool (helm in this case).

I will try using an object instead of just a raw string to see if it serves my case, thank you for suggesting it.

from vitest.

hi-ogawa avatar hi-ogawa commented on August 16, 2024

Thanks for providing the detail. Btw, did you try toMatchSnapshot? https://vitest.dev/api/expect.html#tomatchsnapshot This uses a single file __snapshots__/some-file.test.ts.snap to write all the snapshots for each test file, so I would assume it hinders readability for your use case, but just in case.

from vitest.

dangmai avatar dangmai commented on August 16, 2024

Yeah that's my workaround for now. The downside is that the snapshot file is ~40,000 lines and GitHub has trouble showing the diff when something changes πŸ˜‚

In any case, if you think this issue won't be fixed please feel free to close it. I figure it's better to open this issue so that other folks can search for it if they run into the same thing.

from vitest.

dangmai avatar dangmai commented on August 16, 2024

By the way, for my specific use case, your suggestion of switching from raw string to an object did work - I use a YAML parser to turn the raw string into an object for comparison, and toMatchFileSnapshot does invoke my custom serializer when I do that, so thank you for the suggestion!

from vitest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.