### Snapshot Variants Example Source: https://github.com/getsentry/snapshotpreviews/blob/main/README.md This example demonstrates how to use PreviewVariants to render a view under multiple conditions like dark mode, RTL, and large text. Each previewVariant becomes a separate snapshot. ```swift struct MyView_Previews: PreviewProvider { static var previews: some View { PreviewVariants(layout: .sizeThatFits) { MyView(mode: .loaded) .previewVariant(named: "My View - Loaded") MyView(mode: .loading) .previewVariant(named: "My View - Loading") MyView(mode: .error) .previewVariant(named: "My View - Error") } } } ``` -------------------------------- ### Basic SnapshotTest Class Setup Source: https://github.com/getsentry/snapshotpreviews/blob/main/README.md Create a test class inheriting from SnapshotTest to generate snapshots from Xcode previews. No test functions are needed as they are generated at runtime. ```swift import SnapshottingTests class DemoAppPreviewTest: SnapshotTest { // Optional: return preview type names like "MyApp.MyView_Previews" to render only a subset. override class func snapshotPreviews() -> [String]? { return nil } // Optional: exclude specific previews from rendering. override class func excludedSnapshotPreviews() -> [String]? { return nil } } ``` -------------------------------- ### Migrated Snapshot Test Example Source: https://github.com/getsentry/snapshotpreviews/blob/main/Examples/UnitTestMigration/README.md This Swift code demonstrates a migrated unit test. It inherits from SnapshotTest and conforms to PreviewProvider, automatically converting snapshot assertions into SwiftUI previews. Ensure the SnapshotTest base class is added to your project. ```swift import SwiftUI // Add the conformance to `PreviewProvider` when extending `SnapshotTest` // The `SnapshotTest` base class automatically handles turning calls to // assertSnapshot into previews. class ExampleSnapshotTest: SnapshotTest, PreviewProvider { func testContentViewSnapshot() { assertSnapshot(of: ContentView(), as: .image) } } ``` -------------------------------- ### Add SnapshotPreviews as a Swift Package Manager Dependency Source: https://github.com/getsentry/snapshotpreviews/blob/main/README.md To install SnapshotPreviews, add the repository URL to your Swift Package Manager dependencies. ```bash https://github.com/EmergeTools/SnapshotPreviews ``` -------------------------------- ### Run Xcode Tests from Command Line Source: https://github.com/getsentry/snapshotpreviews/blob/main/Examples/MultiModuleDemo/README.md Execute Xcode tests for the MultiModuleDemo scheme, specifying the output directory for snapshots and excluding specific test cases. This command is useful for automated testing and CI environments. ```bash TEST_RUNNER_SNAPSHOTS_EXPORT_DIR=/tmp/snapshots xcodebuild test \ -scheme MultiModuleDemo \ -sdk iphonesimulator \ -destination 'platform=iOS Simulator,name=iPhone 17 Pro Max' \ -skip-testing:MultiModuleDemoTests/ModuleFilterAssertionTests \ CODE_SIGNING_ALLOWED=NO \ | xcpretty ``` -------------------------------- ### Presenting the Preview Gallery in SwiftUI Source: https://github.com/getsentry/snapshotpreviews/blob/main/README.md Integrate the PreviewGallery into your SwiftUI app to create a browsable gallery of components. This is useful for internal builds. ```swift import SwiftUI import PreviewGallery struct InternalSettingsView: View { var body: some View { NavigationStack { Form { Section("Previews") { NavigationLink("Open Gallery") { PreviewGallery() } } } } .navigationTitle("Internal Settings") } } ``` -------------------------------- ### Configure Snapshot Previews in Swift Source: https://github.com/getsentry/snapshotpreviews/blob/main/README.md Use snapshot modifiers to customize tags, additional context, and diff thresholds for individual previews. Ensure SnapshotPreferences is imported. ```swift import SnapshotPreferences #Preview("Map") { MapPreview() .snapshotTags(["screen": "map"]) .snapshotAdditionalContext(["fixture": "city-route"]) .snapshotDiffThreshold(0.05) } ``` -------------------------------- ### GitHub Actions Step for Snapshot Tests Source: https://github.com/getsentry/snapshotpreviews/blob/main/README.md A complete GitHub Actions workflow step to run snapshot tests and upload the results to Sentry. It configures the export directory and uses secrets for the auth token. ```yaml - name: Run snapshot tests env: TEST_RUNNER_SNAPSHOTS_EXPORT_DIR: ${{ github.workspace }}/snapshot-images run: | xcodebuild test \ -scheme MyApp \ -sdk iphonesimulator \ -destination 'platform=iOS Simulator,name=iPhone 15 Pro' - name: Upload snapshots to Sentry env: SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }} run: | sentry-cli build snapshots "$GITHUB_WORKSPACE/snapshot-images" \ --app-id com.example.MyApp \ --project my-ios-project ``` -------------------------------- ### Filter Snapshots by Module Source: https://github.com/getsentry/snapshotpreviews/blob/main/README.md Configure which modules to include or exclude from snapshot generation. This is useful for large projects with multiple frameworks. ```swift // Only snapshot previews from these modules override class func snapshotPreviewModules() -> [String]? { ["MyFeatureModule"] } // Skip previews from these modules override class func excludedSnapshotPreviewModules() -> [String]? { ["LegacyModule"] } ``` -------------------------------- ### Upload Snapshots with sentry-cli Source: https://github.com/getsentry/snapshotpreviews/blob/main/README.md Upload exported snapshot images to Sentry using sentry-cli. Provide the path to the exported images, an auth token, app ID, and project name. ```bash sentry-cli build snapshots "$PWD/snapshot-images" \ --auth-token "$SENTRY_AUTH_TOKEN" \ --app-id com.example.MyApp \ --project my-ios-project ``` -------------------------------- ### Detect Running Previews Source: https://github.com/getsentry/snapshotpreviews/blob/main/README.md Use this extension to check if the current environment is running Xcode Previews. This helps disable preview-unfriendly behavior like logging or network calls. ```swift extension ProcessInfo { var isRunningPreviews: Bool { environment["SNAPSHOTS_RUNNING_FOR_PREVIEWS"] == "1" } } ``` -------------------------------- ### SwiftUI View Preview Source Interface Source: https://github.com/getsentry/snapshotpreviews/blob/main/PreviewsSupport/README.md Add this struct definition to the end of SwiftUI's .swiftinterface files to enable ViewPreviewSource extraction. ```swift @available(iOS 17.0, macOS 14.0, *) public struct ViewPreviewSource { public var makeView: @_Concurrency.MainActor () -> any SwiftUI.View } ``` -------------------------------- ### UIKit UIView Preview Source Interface Source: https://github.com/getsentry/snapshotpreviews/blob/main/PreviewsSupport/README.md Add this struct definition to the end of UIKit's .swiftinterface files for UIView preview extraction. ```swift @available(iOS 17.0, macOS 14.0, *) public struct UIViewPreviewSource { public var makeView: @_Concurrency.MainActor () -> UIKit.UIView } ``` -------------------------------- ### Set Unique Display Names for Previews Source: https://github.com/getsentry/snapshotpreviews/blob/main/README.md Ensure each preview has a unique display name for clarity in test results and exported files. This applies to both PreviewProvider and #Preview macro usage. ```swift struct MyView_Previews: PreviewProvider { static var previews: some View { MyView().previewDisplayName("My Display Name") } } #Preview("My Display Name") { MyView() } ``` -------------------------------- ### UIKit UIViewController Preview Source Interface Source: https://github.com/getsentry/snapshotpreviews/blob/main/PreviewsSupport/README.md Add this struct definition to the end of UIKit's .swiftinterface files for UIViewController preview extraction. ```swift @available(iOS 17.0, macOS 14.0, *) public struct UIViewControllerPreviewSource { public var makeViewController: @_Concurrency.MainActor () -> UIKit.UIViewController } ``` -------------------------------- ### Export Snapshots with xcodebuild Source: https://github.com/getsentry/snapshotpreviews/blob/main/README.md Set the TEST_RUNNER_SNAPSHOTS_EXPORT_DIR environment variable to specify the output directory for snapshot images when running xcodebuild tests. This directs images to a specified directory instead of the .xcresult bundle. ```bash TEST_RUNNER_SNAPSHOTS_EXPORT_DIR="$PWD/snapshot-images" \ xcodebuild test \ -scheme MyApp \ -sdk iphonesimulator \ -destination 'platform=iOS Simulator,name=iPhone 15 Pro' ``` -------------------------------- ### Implementing Accessibility Audits with XCTest Source: https://github.com/getsentry/snapshotpreviews/blob/main/README.md Inherit from AccessibilityPreviewTest to run Xcode accessibility audits on your previews as part of a UI test. Override auditType and handle issues as needed. ```swift import SnapshottingTests import Snapshotting class DemoAppAccessibilityPreviewTest: AccessibilityPreviewTest { override func auditType() -> XCUIAccessibilityAuditType { return .all } override func handle(issue: XCUIAccessibilityAuditIssue) -> Bool { return false } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.