Snapshot & Preview-Driven Testing
Catch visual regressions across states.
Snapshot & Preview-Driven Testing is a free SwiftUI Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Catching Visual Bugs
Some bugs are not crashes; they are layout slips you only see. Snapshot tests catch these by comparing images of your views. 👀
How Snapshots Work
A snapshot test renders a view to an image once, saves it, then fails later if a new render no longer matches.
The Reference Image
The first run records a trusted reference image. You review it by eye, and from then on it becomes the source of truth.
Previews Drive Testing
SwiftUI previews already show a view in many states. Designing those states first makes your snapshots easy to write.
#Preview { ProfileCard(user: .sample) }Cover Many States
Add previews for empty, loading, and error states. Each one becomes a snapshot, so regressions in any state are caught.
Test Light and Dark
Render each view in light and dark mode. A color that vanishes in dark mode is exactly what a snapshot will reveal. 🌗
.preferredColorScheme(.dark)Vary Dynamic Type
Snapshot a view at the largest Dynamic Type size too. Text that overflows or clips will fail the comparison immediately.
Pin the Device Size
Always snapshot on a fixed device or frame. Different screen sizes produce different images and cause false failures.
Reading a Failure
When a snapshot fails, the tool shows old, new, and a diff. You decide: a real bug, or an intended change to re-record.
Commit Your Snapshots
Store reference images in version control. Teammates then share the same baseline, and CI can catch visual changes in pull requests.
Re-Record on Purpose
Changed the design intentionally? Switch to record mode once to refresh the baselines, then turn it off again.
Quick Check
What does a snapshot test actually compare?
Recap
You learned snapshot testing: record a reference, cover many preview states, test light and dark, and commit baselines so visual bugs never sneak in. 🎉
Frequently asked questions
Is the “Snapshot & Preview-Driven Testing” lesson free?
Yes — the full text of “Snapshot & Preview-Driven Testing” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Snapshot & Preview-Driven Testing”?
Catch visual regressions across states. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Snapshot & Preview-Driven Testing” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Unit Testing ViewModels
- Testing Async Code
- UI Tests with XCUITest
- Snapshot & Preview-Driven Testing