| name | snapshot |
| description | Creates Snapshot Tests for SwiftUI Views using ChallengeSnapshotTestKit. Use when creating visual regression tests for views with DSAsyncImage support. |
Skill: Snapshot Tests
Guide for creating Snapshot Tests using ChallengeSnapshotTestKit. Tests only use ChallengeSnapshotTestKit's public API. See the module README for internal details.
References
Prerequisites
ChallengeSnapshotTestKit dependency in snapshot test targets (added automatically by Tuist)
DSAsyncImage component (replaces AsyncImage)
ImageLoaderMock in CoreMocks
- Test image in
Tests/Shared/Resources/
File Structure
Tests/
├── Snapshots/
│ └── Presentation/
│ └── {Name}/
│ ├── {Name}ViewSnapshotTests.swift
│ └── __Snapshots__/
└── Shared/
├── Stubs/
│ └── {Name}ViewModelStub.swift
└── Resources/
└── test-avatar.jpg
Test Structure
Uses instance variables pattern for cleaner tests:
struct {Name}ViewSnapshotTests {
private let imageLoader: ImageLoaderMock
init() {
UIView.setAnimationsEnabled(false)
imageLoader = ImageLoaderMock(cachedImage: .stub, asyncImage: .stub)
}
@Test("Renders loading state correctly")
func loadingState() {
let viewModel = {Name}ViewModelStub(state: .loading)
let view = NavigationStack {
{Name}View(viewModel: viewModel)
}
.imageLoader(imageLoader)
assertSnapshot(of: view, as: .device)
}
}
Key Rules
Test Setup
- Disable animations:
UIView.setAnimationsEnabled(false)
- Create
ImageLoaderMock with test image
- Inject
.imageLoader(imageLoader) on view
View Configuration
- Wrap in
NavigationStack
- Use
.device strategy for full-screen views, .image for components, .component(size:) for components that wrap a Button
- Apply
imageLoader modifier
Naming
- Test file:
{Name}ViewSnapshotTests.swift
- Test method:
{stateName}State() (e.g., loadingState)
- Test description:
@Test("Renders {state} state correctly")
- Snapshots folder:
__Snapshots__/{Name}ViewSnapshotTests/
Running Tests
First Run (Recording)
xcodebuild test \
-workspace Challenge.xcworkspace \
-scheme "Challenge (Dev)" \
-destination 'platform=iOS Simulator,name=iPhone 17 Pro,OS=26.1'
First run creates references and fails (expected).
Subsequent Runs
Re-run the same command. Snapshots now compare against recorded references.
Regenerate Snapshots
rm -rf Tests/Snapshots/Presentation/{Name}/__Snapshots__
Checklist
Setup (once per feature)
Per View