One of the most common ways to write test tests in React is through snapshot tests. They are very convenient, easy to use, and require little to no testing effort. But over the past years, they have fallen out of favour more and more in the field of React. Most of the people in the community have concluded that snapshot tests barely hold any meaningful value. Whereas behaviour tests that are reflective of how users interact with your application are more reliable, realistic, and useful.
Due to this, most React developers have gotten rid of snapshot tests entirely and migrated their testing codebase towards behaviour tests focused style. Nowadays, snapshot testing is often viewed as a fake form of testing, only providing a false sense of security. You’ll often hear that unless you have specific reasons to perform snapshot testing, it’s recommended to stay away from them.
After hearing that piece of advice over and over again, I knew that I shouldn’t use snapshot testing as a default and that it only really had specific use cases. But I didn’t understand when I should use it. What are use cases for snapshot testing? When do they make sense? What are they actually useful for?
Over the past months, I’ve looked out for these use cases while developing React components. I wanted to see in which scenarios snapshot tests would make more sense than standard unit or behaviour tests. After spending quite some time on it, I finally understood the underlying issue with snapshot tests. With this, I realised why snapshot tests were abandoned and what makes them so meaningless, but also in which use cases they are most relevant. This article will that knowledge with you.