Jest: Use difftool to resolve snapshot differences

Created on 16 Oct 2016  路  11Comments  路  Source: facebook/jest

I maintain an assertion library for .net which has a very similar feature to snapshot testing in Jest and have been a huge fan of this style of testing for ages. The docs for the feature is at http://shouldly.readthedocs.io/en/latest/assertions/shouldMatchApproved.html, I took inspiration from ApprovalTests which also has a NodeJS version. https://github.com/approvals/Approvals.NodeJS

For Jest, in addition to the update snapshot parameter it would be great to have a --resolve or --merge switch which would write out a .received.snap file next to the actual then launch a difftool in merge mode, the user can easily merge the received.snap file into the .snap file. Shouldly and ApprovalTests just have a collection of supported difftools and will simply use the first one it finds.

I started to spike this out tonight as an extension to expect (expect-jest-snapshot), then I noticed #1679 so I thought I would hold off and start a conversation about the idea first. What do you think?

All 11 comments

I have used both ApprovalTests and shouldly and this style of testing is great when your dealing with message contracts or api responses, but the use-cases doesn't stop there. making use of visual compare tool would be an amazing feature.

We were hoping to build an interactive mode for Jest where it will ask to update each snapshot individually. Would that solve your problem?

It would, I wonder if just using diff tools would be a more flexible first pass? How would it work?

@JakeGinnivan since Jest 17 larger diffs are shown in a form of a patch, so I think we're on a good track.
@cpojer Should we create a separate issue for interactive snapshots update mode and maybe continue there?

Yeah, let's put that in a separate issue. It's on our plans for Jest but I don't know if we'll get to it anytime soon.

That patch form is awesome!

I still think just launching a difftool would be a really simple way to do interactive snapshot updates for the time being. They already do everything you need very well, and people have their preferred tools to do merging.

@cpojer Did this issue go anywhere? It seems like it got closed but with no resolution. I want to second the OP's original request to provide a mechanism for using an external diff tool to resolve snapshot failures. I personally hate the unified style diffs that we get now when there are some great diff tools like Beyond Compare that can make it much easier to see what changed.

Interactive snapshots are available: https://facebook.github.io/jest/docs/en/snapshot-testing.html#interactive-snapshot-mode

@SimenB Thanks for that but that doesn't quite answer my question. The request is for making use of an external diff tool, not just a CLI based interactive tool.

@JakeGinnivan did you ever get something working with Beyond Compare (or similar)?

Never dug in and sorted it out unfortunately @flcdrg

Was this page helpful?
0 / 5 - 0 ratings