Snapshot testing
What is Snapshot Testing?
Snapshot testing is a type of âoutput comparisonâ or âgolden masterâ testing. These tests prevent regressions by comparing the current characteristics of an application or component with stored âgoodâ values for those characteristics. Snapshot tests are fundamentally different from unit and functional tests. While these types of tests make assertions about the correct behavior of an application, snapshot tests only assert that the output now is the same as the output before; it says nothing about whether the behavior was correct in either case.
The test doesnât say whether the old or new stylings were correct, it just highlights to the tester that something has changed.
A variety of characteristics can be measured, but front-end tests typically focus on two:
data (serializable JavaScript values)
images.
How Snapshot Testing Works
Snapshot tests work by recording some characteristic of a system (e.g., taking a snapshot), and then later comparing that stored snapshot to the current value of the characteristic. For Jest-style tests, that characteristic is typically a serialized render tree. The first time a test is run, the toMatchSnapshot expectation saves the data it receives to a file. During later test runs, the current value of elem is compared to the stored âgoodâ one. The test fails if the two values are different. Updating snapshots is very easy, making them very maintainable, but this can be both a blessing and a curse.
The Benefits of Snapshot Testing
Snapshot testing is the cheapest and fastest way to increase code coverage. It's a great way to check that your applicationâs behavior isnât changing unexpectedly during development. (prevents regressions in the app)
Snapshot tests can help out quite a bit in that situation because theyâre typically much shorter and easier to write than traditional unit tests.
Snapshot tests are also easy to keep up to date as developers generally just need to run a single command to get the testing system to record new snapshots. This is certainly much easier than needing to edit many test files to bring tests back in sync with reality.
The Drawbacks of Snapshot Testing
A significant disadvantage is that theyâre tightly coupled to an applicationâs output, making them very fragile. Any changes, even to insignificant parts of the output, can cause snapshot tests to fail. Developers then must (or at least should) manually verify that everything is still working properly and update the snapshots.
They are generated files, and developers tend to be undisciplined about scrutinizing generated files before committing them, if not at first then definitely over time. Most developers, upon seeing a snapshot test fail, will sooner just nuke the snapshot and record a fresh passing one instead of agonizing over what broke it.
This leads to another potential problem with snapshot tests: they donât actually indicate anything about the expected output, just that it shouldnât change. Unlike unit and functional tests, snapshot tests donât contain focused, meaningful assertions or expectations. A developer who has to manually verify that the output is still âgoodâ may run into trouble when the failing test is for a part of the app he or she isnât familiar with, because a snapshot test doesnât indicate what parts of the output are important.
Snapshot tests arenât inherently well suited to dynamic content. A ârandom quote of the dayâ component will frequently fail snapshot tests since the random quote in the output usually wonât match the stored component data. Tools can deal with this problem by letting users mark areas with dynamic content.
Another potential drawback to snapshot tests is storage requirements. Snapshots must be stored somewhere; typically theyâre checked into the project repository. Depending on what characteristic is being recorded, snapshots can be quite large. Render trees are text-based, and generally diff and compress well, but an extensive suite of screenshot-based snapshot tests can easily consume tens of megabytes of space. This is another area where cloud-based services can provide some assistance by storing snapshots for visual tests.
Last updated