Skip to content

Commit

Permalink
Register test observer in main queue
Browse files Browse the repository at this point in the history
Fixes a crash when calling [XCTestObservationCenter addTestObserver:] that would fail an assertion: 'Test observers can only be registered and unregistered on the main thread.'
  • Loading branch information
MarkVillacampa committed Feb 29, 2024
1 parent e7b7722 commit 206370d
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions Sources/SnapshotTesting/AssertSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 206370d

Please sign in to comment.