DEV Community

Cover image for When should you use test snapshots?
Samuel Durante for Woovi

Posted on

When should you use test snapshots?

At Woovi we have more than 6000 tests. Constantly comes the doubt if we must use snapshots or not.

What is a snapshot?

Snapshot is a way of storing the expected return to guarantee that the return has always been the same.

When can you use a snapshot?

Let's imagine that we have an API, and we need all the fields to be defined, instead of doing this next:

expect(response.body.value).toBeDefined();
expect(response.body.expiresIn).toBeDefined();
expect(response.body.correlationID).toBeDefined();
...
Enter fullscreen mode Exit fullscreen mode

We can use a snapshot in response.body, guaranteeing that all fields will be defined.

expect(response.body).toMatchSnapshot();
Enter fullscreen mode Exit fullscreen mode
exports[`should return 3 fields 1`] = `
{
  "value": 1000,
  "expiresIn": 2592000,
  "correlationID": "correlationID",
}
`;
Enter fullscreen mode Exit fullscreen mode

When can't you use a snapshot?

We saw that snapshots are good for verifying many fields, but otherwise, they are bad when you want to test a specific field.

For example, we need to verify if only 1 field is defined, instead of taking a snapshot, we can verify this field directly, like this:

expect(request.body.expiresDate).toBeDefined();
Enter fullscreen mode Exit fullscreen mode

Taking snapshots for these cases makes testing confusing because you don't know the fields you are testing.


Woovi is a Startup that enables shoppers to pay as they like. To make this possible, Woovi provides instant payment solutions for merchants to accept orders.

If you want to work with us, we are hiring!


Photo by Alphacolor on Unsplash

Top comments (0)