Swift Testing for Compound (#5110)

This commit is contained in:
Mauro
2026-02-16 14:04:04 +01:00
committed by GitHub
parent 26bfa6ed7c
commit e8a6af1956
7 changed files with 135 additions and 122 deletions
@@ -8,12 +8,12 @@
import Combine
@testable import Compound
@preconcurrency @testable import SnapshotTesting
@testable import SnapshotTesting
import SwiftUI
import XCTest
import Testing
@MainActor
class PreviewTests: XCTestCase {
@Suite(.serialized)
struct PreviewTests {
private struct SnapshotDevice {
let name: String
let device: String
@@ -28,17 +28,13 @@ class PreviewTests: XCTestCase {
.init(name: "iPad", device: "iPad")]
private var recordMode: SnapshotTestingConfiguration.Record = .missing
override func setUp() async throws {
try await super.setUp()
await MainActor.run {
if ProcessInfo().environment["RECORD_FAILURES"].map(Bool.init) == true {
recordMode = .failed
}
checkEnvironments()
UIView.setAnimationsEnabled(false)
init() {
if ProcessInfo().environment["RECORD_FAILURES"].map(Bool.init) == true {
recordMode = .failed
}
checkEnvironments()
UIView.setAnimationsEnabled(false)
}
/// Check environments to avoid problems with snapshots on different devices or OS.
@@ -72,8 +68,7 @@ class PreviewTests: XCTestCase {
let imageRenderer = ImageRenderer(content: preferenceReadingView)
_ = imageRenderer.uiImage
var sanitizedSuiteName = String(testName.suffix(testName.count - "test".count).dropLast(2))
sanitizedSuiteName = sanitizedSuiteName.prefix(1).lowercased() + sanitizedSuiteName.dropFirst()
let sanitizedSuiteName = String(testName.dropLast(2))
for snapshotDevice in snapshotDevices {
guard var device = PreviewDevice(rawValue: snapshotDevice.device).snapshotDevice() else {
@@ -102,7 +97,7 @@ class PreviewTests: XCTestCase {
testName: sanitizedSuiteName,
traits: traits,
preferences: preferences) {
XCTFail(failure)
Issue.record(Comment(rawValue: failure))
}
}
}
@@ -142,14 +137,6 @@ class PreviewTests: XCTestCase {
testName: testName)
}
}
private func wait(for duration: TimeInterval) {
let expectation = XCTestExpectation(description: "Wait")
DispatchQueue.main.asyncAfter(deadline: .now() + duration) {
expectation.fulfill()
}
_ = XCTWaiter.wait(for: [expectation], timeout: duration + 1)
}
}
private class SnapshotPreferences: @unchecked Sendable {