Do you do Snapshot Testing?
Tags: discuss, testing, bestpractice, codequality
Published: 2020-10-20 06:36:52

---
title: Do you do Snapshot Testing?
published: true
description: Discuss the pro and cons of snapshot testing
tags: #discuss, #testing, #bestpractice, #codequality
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/5w1eo3pjck1bdgmvlano.png
---

And more specifically do you write Snapshot tests for your Infrastructure as Code ( like AWS CDK stacks)?

### The context

> Snapshot testing is a type of “output comparison” or “golden master” testing. 

Basically, you freeze a specific state/view/characteristic of your application and compare it overtime to make sure it did not change.  **Snapshot tests do not test the behavior**, the fact that with a given input the output is always the same. 
They just make sure that the current state/representation is the same **as the one you have _stored_**. If it is not, you are asked if you want to overwrite the saved snapshot and replace it with the current result.

Over the years **I can count on the fingers of one hand**, the times when I actually used Snapshot tests:
  - when we were refactoring the basic engine of one of our games and we wanted to be really really sure we weren't changing anything of how that complex UI was looking.  Once the refactoring was done,  we dropped the snapshot testing because changes to the UI were part of every sprint, so it did not make sense. 
  - when we just started with React, and we were really excited to take advantage of the Snapshot testing functionality for our React components.
  - when we started using AWS CDK to build our Serverless Infrastructure. In this case, the snapshot is not that "Visual", like a picture of the UI, rather a comparison of the JSON output of Cloudformation.

## The problem:

As soon as you change something, not even in the stack itself but in the lambda code (since the hash of the uploaded asset will be different), the test would fail. Therefore, you have to rerun the test, forcing/confirming and you have to run the test locally,  confirm the update of the snapshot, and push.  

The difference with Unit ( or integration tests etc) is that with those, usually, you have to update the tests only if something changes **either in the inputs or in the expected outputs.**  
That is why we have tests in the first place,  **to make sure that refactorings of the code itself won't affect behavior from the outside.**

I understand that if you add a new resource to your API, or change a policy to write to your bucket, you are modifying the architecture of your application, I understand that it requires an additional action from the developer to intentionally edit the snapshots. and I know that the snapshots will be version controlled, so you can quickly compare all the final deployed stacks. But I still don't see much benefit in it. 

It could be handy for some small things like (very) small react components returning confirmation or error messages ( instead of using regex to check for the match,  match the snapshot) 
https://kentcdodds.com/blog/effective-snapshot-testing

But for the entire stack or the entire screenshot of an application, snapshot updates would be required for ANY single change,  so it will simply become part of the routine:
- change code
- run test
- get the warning snapshot test failed
- update snapshot 
- commit
without much thinking. Therefore making the test **useless**.

## The Question
I do not have yet a strong opinion about such tests, and I think it is nice to have them especially for some specific _critical happy paths_ in your UI (like the login form, or shop page or for small CDK Constructs (that maybe you share with other teams), but it really adds a burden of work, and often gives **a false sense of security**, and when it comes to test the CDK I much prefer to have a smaller test checking for the existence of a specific Resource / Role rather than Snapshot test the entire thing)

Do you use them,  how far do you go with them?  
How extensively do you snapshot test your application?  
How long have you been doing it (in some project I worked on, it was introduced, but then dropped just after a few weeks, for the reasons I mentioned above)

### Reference

- [Improving Communication and Confidence with Snapshot tests](https://engineering.klarna.com/improving-communication-and-confidence-with-visual-snapshot-testing-b04154c3aaf0)
- [Recognize the need for visual testing](https://docs.cypress.io/guides/tooling/visual-testing.html#Recognize-the-need-for-visual-testing)
- [Jest Image Snapshot Docs](https://github.com/americanexpress/jest-image-snapshot)
- [ How to test AWS CDK](https://docs.aws.amazon.com/cdk/latest/guide/testing.html)
- [Benefits and Drawbacks of Snapshot testing](https://www.sitepen.com/blog/snapshot-testing-benefits-and-drawbacks)


-----
<span>Photo by <a href="https://unsplash.com/@brittanyhopegaiser?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">britt gaiser</a> on <a href="https://unsplash.com/photos/D1HtB-o7eK8?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></span>