From 206370d0ed9a3e30e5e30b2ea46363a551b5c094 Mon Sep 17 00:00:00 2001 From: Mark Villacampa Date: Thu, 29 Feb 2024 12:32:40 +0100 Subject: [PATCH] Register test observer in main queue Fixes a crash when calling [XCTestObservationCenter addTestObserver:] that would fail an assertion: 'Test observers can only be registered and unregistered on the main thread.' --- Sources/SnapshotTesting/AssertSnapshot.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Sources/SnapshotTesting/AssertSnapshot.swift b/Sources/SnapshotTesting/AssertSnapshot.swift index 552077e49..e144f6a15 100644 --- a/Sources/SnapshotTesting/AssertSnapshot.swift +++ b/Sources/SnapshotTesting/AssertSnapshot.swift @@ -371,11 +371,9 @@ func sanitizePathComponent(_ string: String) -> String { // We need to clean counter between tests executions in order to support test-iterations. private class CleanCounterBetweenTestCases: NSObject, XCTestObservation { private static var registered = false - private static var registerQueue = DispatchQueue( - label: "co.pointfree.SnapshotTesting.testObserver") static func registerIfNeeded() { - registerQueue.sync { + DispatchQueue.main.sync { if !registered { registered = true XCTestObservationCenter.shared.addTestObserver(CleanCounterBetweenTestCases())