Contents

philiprehberger/swift-snapshot-kit

Snapshot testing for SwiftUI with visual diffs, multi-device support, and dark mode comparison

Requirements

  • Swift >= 6.0
  • macOS 13+ / iOS 16+ / tvOS 16+ / watchOS 9+

Installation

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")
])

Usage

import SnapshotKit

// Basic snapshot test
try assertSnapshot(of: MyView())

// Record mode (first run or to update references)
try assertSnapshot(of: MyView(), record: true)

Custom Configuration

try assertSnapshot(
    of: MyView(),
    as: SnapshotConfig(
        size: DevicePreset.iPhone15Pro.size,
        traits: SnapshotTraits(colorScheme: .dark),
        tolerance: 0.01
    )
)

Multi-Device Testing

for device in [DevicePreset.iPhoneSE, .iPhone15, .iPadPro11] {
    try assertSnapshot(
        of: MyView(),
        as: SnapshotConfig(size: device.size),
        named: device.name
    )
}

Dark Mode Comparison

try assertSnapshot(
    of: MyView(),
    as: SnapshotConfig(traits: SnapshotTraits(colorScheme: .dark)),
    named: "dark"
)

Dynamic Type

try assertSnapshot(
    of: MyView(),
    as: SnapshotConfig(traits: SnapshotTraits(dynamicTypeSize: .xxxLarge)),
    named: "large-text"
)

API

assertSnapshot

| 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 |

SnapshotConfig

| Property | Description | |----------|-------------| | size | The rendering size | | traits | Color scheme and dynamic type size | | tolerance | Acceptable difference (0.0-1.0) |

DevicePreset

| 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 |

Development

swift build
swift test

Support

Bluesky · X · LinkedIn · Website · GitHub · Buy Me a Coffee · GitHub Sponsors

License

MIT

Package Metadata

Repository: philiprehberger/swift-snapshot-kit

Default branch: main

README: README.md