DEV Community

Discussion on: Snapshot Driven Development with Jest

Collapse
 
philou profile image
Philippe Bourgau • Edited

Thanks for showing a good use of Jest Snapshots. Indeed, they can be misused, but they can also come in pretty handy at time. I've also used snapshot effectively when testing legacy code.

Here is another trick I have used successfully in the situation you describe. Very often when you have a format conversion function, you also have the inverse function (it would be the one converting from API to your internal representation). In this case, it can be handy to test the 2 together.

expect(fromAPIFormat(toAPIFormat(input))).toEqual(input))
Enter fullscreen mode Exit fullscreen mode

This way you can test many variations of data without having to resort to snapshot every time. Basically, you can skip snapshots when testing the fromAPIFormat function.

Thanks for your post!

Collapse
 
qmenoret profile image
Quentin Ménoret

Very good point thanks for sharing!