PreviewSnapshots
repository·main·Indexed 41 days ago
https://github.com/doordash-oss/swiftui-preview-snapshotsA 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.
What's inside PreviewSnapshots
- 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.
Install PreviewSnapshots via Swift Package Manager
mainAdd the package as a dependency in your
Package.swiftfile. You must addPreviewSnapshotsto your primary target andPreviewSnapshotsTestingto 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"), ] ) ]Install PreviewSnapshots via Xcode
main- From the File menu, select Add Packages....
- Enter the repository URL:
https://github.com/doordash-oss/swiftui-preview-snapshots. - Confirm the version and allow Xcode to resolve the package.
- 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.
How to use PreviewSnapshots in SwiftUI Previews
mainTo use PreviewSnapshots in your previews, import
PreviewSnapshotsand define a static computedsnapshotsproperty on yourPreviewProviderimplementation.- Define a
PreviewSnapshots<State>property whereStateis the type of data used to configure your view. - Provide an array of
configurationsusing.init(name:state:). - Provide a
configureclosure that takes thestateand returns the configured view. - Return
snapshots.previewsfrom the standardpreviewscomputed 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() } ) } }- Define a
How to use PreviewSnapshots in Snapshot Tests
mainTo run snapshot tests against your preview configurations, import
PreviewSnapshotsTestingand call.assertSnapshots()on yourPreviewProvider'ssnapshotsproperty within anXCTestCase.PreviewSnapshotsTestingwill automatically generate a separate snapshot for each configuration defined in yoursnapshotsproperty and compare them against the reference images on disk. You can also define multiple snapshot collections on a singlePreviewProvider(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() } }