Skip to content
tezvyn:
Menu

Snapshot Testing: Catch Unintended UI Changes

Source: github.comHardHow cards are made

Snapshot testing compares your UI to a saved 'golden' image, failing if pixels differ. It's a visual diff for your views, catching unintended regressions in layout or style. The main footgun is forgetting to re-record snapshots after intentional UI changes.

Why it exists

Traditional unit tests are poor at verifying visual appearance. You can check a label's text, but not its font, color, truncation, and position relative to other elements without writing brittle, complex assertions. Snapshot testing solves this by checking the final rendered output.

The mental model

Think of snapshot testing as a "spot the difference" game played by your CI server. The first time you run the test, you take a picture of your UI and call it "correct." On every subsequent run, you take a new picture and compare it to the original. If there's even a one-pixel difference, the test fails.

How it works

When you first run a snapshot test for a view, the library finds no existing reference. It saves the current rendered output as an image file (the "reference" or "golden" snapshot) and fails the test, instructing you to run it again. On the second run, it renders the view again and compares it pixel-by-pixel to the saved reference image. If they match, the test passes. If not, it fails and often generates a diff image showing the discrepancies. If a change is intentional, you can command the library to re-record the reference, approving the new version.

When to use it

Use snapshot testing for UI components with a complex or critical visual structure. It's perfect for design system components, custom views, table view cells, or entire view controllers. It provides a strong guarantee against visual regressions during code refactoring or when updating shared styles.

When not to use it

Avoid snapshotting views with dynamic, non-deterministic content like live dates, random numbers, or network-loaded data unless you can mock or stub that data consistently. It is not a replacement for unit tests that check logic; it is a complement that checks appearance. Be mindful that snapshots are image files that can increase your repository size.

One canonical example

A common use case is testing a user profile card component. This card might have a user's name, avatar, and bio. A snapshot test ensures that changing a font style in the app's theme doesn't accidentally cause the bio text to overflow its container or overlap with the avatar. If a designer changes the layout intentionally, the developer simply re-records the snapshot to approve the new "golden" version.

Interview question

What is the standard procedure when a snapshot test fails due to an intentional update to the UI's visual appearance?

  • a.Update the test's reference snapshot to match the new, approved UI.Correct
  • b.Convert the snapshot test into a traditional unit test with explicit assertions.
  • c.Revert the UI changes until the test passes with the existing snapshot.
  • d.Mark the failing test as 'skipped' to allow the intentional changes to be merged.
Why?

The card explains that if a UI change is intentional, you must "re-record the reference, approving the new version" to update the 'golden' snapshot. Reverting changes would negate the intentional update, making option C incorrect.

Just read this? Test yourself on what you have been reading.

Read the original → github.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.

See open roles