PreviewSnapshots

repository·main·Indexed 41 days ago

https://github.com/doordash-oss/swiftui-preview-snapshots

A utility for SwiftUI developers to share view configurations between SwiftUI Previews and SnapshotTesting. It allows a single set of configurations to be rendered as individual Xcode previews and as automated snapshot assertions in a test suite to ensure consistency between visual development and testing.

Tokens
1.5K
Snippets
3
Records
5
Agent score
2%

What's inside PreviewSnapshots

  1. What is PreviewSnapshots?

    main
    PreviewSnapshots is a library designed to share view configurations between SwiftUI Previews and SnapshotTesting snapshot tests. It allows you to define a single set of configurations (states) that are automatically rendered as individual SwiftUI Previews in Xcode and as individual snapshot assertions in your test suite, ensuring consistency between visual development and automated testing.
  2. Install PreviewSnapshots via Swift Package Manager

    main

    Add the package as a dependency in your Package.swift file. You must add PreviewSnapshots to your primary target and PreviewSnapshotsTesting to your test target.

    // 1. Add the package dependency
    dependencies: [
        .package(url: "https://github.com/doordash-oss/swiftui-preview-snapshots", from: "1.0.0"),
    ]
    
    // 2. Add products to your targets
    targets: [
        .target(
            name: "MyGreatApp",
            dependencies: [
                .product(name: "PreviewSnapshots", package: "swiftui-preview-snapshots"),
            ]
        ),
        .testTarget(
            name: "MyGreatAppTests",
            dependencies: [
                "MyGreatApp",
                .product(name: "PreviewSnapshotsTesting", package: "swiftui-preview-snapshots"),
            ]
        )
    ]
  3. Install PreviewSnapshots via Xcode

    main
    1. From the File menu, select Add Packages....
    2. Enter the repository URL: https://github.com/doordash-oss/swiftui-preview-snapshots.
    3. Confirm the version and allow Xcode to resolve the package.
    4. In the final dialog:
      • Set the Add to Target column for PreviewSnapshots to your app target.
      • Set the Add to Target column for PreviewSnapshotsTesting to your app's test target.
  4. How to use PreviewSnapshots in SwiftUI Previews

    main

    To use PreviewSnapshots in your previews, import PreviewSnapshots and define a static computed snapshots property on your PreviewProvider implementation.

    1. Define a PreviewSnapshots<State> property where State is the type of data used to configure your view.
    2. Provide an array of configurations using .init(name:state:).
    3. Provide a configure closure that takes the state and returns the configured view.
    4. Return snapshots.previews from the standard previews computed property.

    Xcode will then generate an individual SwiftUI Preview for every configuration provided.

    import PreviewSnapshots
    import SwiftUI
    
    struct ContentView: View {
        var message: String
        var isEnabled: Bool
    
        var body: some View { ... }
    }
    
    // MARK: - Previews
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            snapshots.previews.previewLayout(.sizeThatFits)
        }
    
        static var snapshots: PreviewSnapshots<String> {
            PreviewSnapshots(
                configurations: [
                    .init(name: "Short Message", state: "Test"),
                    .init(name: "Medium Message", state: "Medium length message"),
                    .init(name: "Long Message", state: "This is a much longer message than the other messages being tested"),
                ],
                configure: { state in
                    ContentView(message: state, isEnabled: true).padding()
                }
            )
        }
    }
  5. How to use PreviewSnapshots in Snapshot Tests

    main

    To run snapshot tests against your preview configurations, import PreviewSnapshotsTesting and call .assertSnapshots() on your PreviewProvider's snapshots property within an XCTestCase.

    PreviewSnapshotsTesting will automatically generate a separate snapshot for each configuration defined in your snapshots property and compare them against the reference images on disk. You can also define multiple snapshot collections on a single PreviewProvider (e.g., disabledSnapshots) to group different test scenarios.

    import PreviewSnapshotsTesting
    import XCTest
    
    @testable import ContentModule
    
    final class ContentViewSnapshotTests: XCTestCase {
        func test_snapshots() {
            // Asserts all configurations defined in the snapshots property
            ContentView_Previews.snapshots.assertSnapshots()
        }
    
        func test_disabled_snapshots() {
            // Asserts a different collection of snapshots defined on the same provider
            ContentView_Previews.disabledSnapshots.assertSnapshots()
        }
    }