Snapshot testing for SwiftUI with visual diffs, multi-device support, and dark mode comparison
.package(url: "https://github.com/philiprehberger/swift-snapshot-kit.git", from: "0.1.0")Snapshot testing for SwiftUI with visual diffs, multi-device support, and dark mode comparison
Add to your Package.swift:
dependencies: [
.package(url: "https://github.com/philiprehberger/swift-snapshot-kit.git", from: "0.1.0")
]
Then add "SnapshotKit" to your test target dependencies:
.testTarget(name: "YourTests", dependencies: [
.product(name: "SnapshotKit", package: "swift-snapshot-kit")
])
import SnapshotKit
// Basic snapshot test
try assertSnapshot(of: MyView())
// Record mode (first run or to update references)
try assertSnapshot(of: MyView(), record: true)
try assertSnapshot(
of: MyView(),
as: SnapshotConfig(
size: DevicePreset.iPhone15Pro.size,
traits: SnapshotTraits(colorScheme: .dark),
tolerance: 0.01
)
)
for device in [DevicePreset.iPhoneSE, .iPhone15, .iPadPro11] {
try assertSnapshot(
of: MyView(),
as: SnapshotConfig(size: device.size),
named: device.name
)
}
try assertSnapshot(
of: MyView(),
as: SnapshotConfig(traits: SnapshotTraits(colorScheme: .dark)),
named: "dark"
)
try assertSnapshot(
of: MyView(),
as: SnapshotConfig(traits: SnapshotTraits(dynamicTypeSize: .xxxLarge)),
named: "large-text"
)
| Parameter | Description |
|---|---|
of | The SwiftUI view to snapshot |
as | Snapshot configuration (size, traits, tolerance) |
named | Optional name for the snapshot file |
record | Set to true to record new reference images |
timeout | Maximum time to wait for rendering |
| Property | Description |
|---|---|
size | The rendering size |
traits | Color scheme and dynamic type size |
tolerance | Acceptable difference (0.0-1.0) |
| Preset | Size |
|---|---|
iPhone15 | 393 x 852 |
iPhone15Pro | 393 x 852 |
iPhone15ProMax | 430 x 932 |
iPhoneSE | 375 x 667 |
iPadPro11 | 834 x 1194 |
iPadPro13 | 1024 x 1366 |
swift build
swift test
Bluesky · X · LinkedIn · Website · GitHub · Buy Me a Coffee · GitHub Sponsors