Skip to content

Instantly share code, notes, and snippets.

@tovkal
Created June 10, 2021 16:20
Show Gist options
  • Save tovkal/0eeb8169b4b1f53a5c0896e5645b7656 to your computer and use it in GitHub Desktop.
Save tovkal/0eeb8169b4b1f53a5c0896e5645b7656 to your computer and use it in GitHub Desktop.
Workaround M1 swift-snapshot-testing
import UIKit
import SnapshotTesting
private let precision: Float = 0.9
// MARK: - UIViewController
extension Snapshotting where Value == UIViewController, Format == UIImage {
/// A snapshot strategy for comparing views based on pixel equality.
public static var impreciseImage: Snapshotting {
return .impreciseImage()
}
/// A snapshot strategy for comparing view controller views based on pixel equality.
///
/// - Parameters:
/// - config: A set of device configuration settings.
/// - size: A view size override.
/// - traits: A trait collection override.
public static func impreciseImage(
on config: ViewImageConfig,
size: CGSize? = nil,
traits: UITraitCollection = .init()
)
-> Snapshotting {
return .image(
on: config,
precision: precision,
size: size,
traits: traits
)
}
/// A snapshot strategy for comparing view controller views based on pixel equality.
///
/// - Parameters:
/// - drawHierarchyInKeyWindow: Utilize the simulator's key window in order to render `UIAppearance` and `UIVisualEffect`s.
/// This option requires a host application for your tests and will _not_ work for framework test targets.
/// - size: A view size override.
/// - traits: A trait collection override.
public static func impreciseImage(
drawHierarchyInKeyWindow: Bool = false,
size: CGSize? = nil,
traits: UITraitCollection = .init()
)
-> Snapshotting {
return .image(
drawHierarchyInKeyWindow: drawHierarchyInKeyWindow,
precision: precision,
size: size,
traits: traits
)
}
}
// MARK: - UIView
extension Snapshotting where Value == UIView, Format == UIImage {
/// A snapshot strategy for comparing views based on pixel equality.
public static var impreciseImage: Snapshotting {
return .impreciseImage()
}
/// A snapshot strategy for comparing views based on pixel equality.
///
/// - Parameters:
/// - drawHierarchyInKeyWindow: Utilize the simulator's key window in order to render `UIAppearance` and `UIVisualEffect`s.
/// This option requires a host application for your tests and will _not_ work for framework test targets.
/// - size: A view size override.
/// - traits: A trait collection override.
public static func impreciseImage(
drawHierarchyInKeyWindow: Bool = false,
size: CGSize? = nil,
traits: UITraitCollection = .init()
)
-> Snapshotting {
return .image(
drawHierarchyInKeyWindow: drawHierarchyInKeyWindow,
precision: precision,
size: size,
traits: traits
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment