Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 9, 2022 11:56 am GMT

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();...

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

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

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();

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


Original Link: https://dev.to/woovi/when-should-you-use-test-snapshots-2h7m

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To