Swift Testing for Unit Tests PART 1 (#5119)

* migrated a lot of unit tests to Swift Testing and added a new implementation for deferred fulfillment

more tests migration

Cleaned the code manually to establish some good patterns

more code improvements

some more code improvements

removed empty tests

update project

* more pr suggestions and cleanups

* removed the TestSetup pattern

* fixing claude not reusing tests

* pr suggestion + added indent rule to swiftformat so that we can prevent AIs to change that
This commit is contained in:
Mauro
2026-02-19 16:20:47 +01:00
committed by GitHub
parent fc03e05697
commit 7982aef855
118 changed files with 4630 additions and 4129 deletions
@@ -7,13 +7,16 @@
//
@testable import ElementX
import XCTest
import Observation
import Testing
@MainActor
class DeferredFulfillmentTests: XCTestCase {
@Suite
struct DeferredFulfillmentTests {
private let observable = SomeObservable()
func testObservableWithoutUpdate() async throws {
@Test
func observableWithoutUpdate() async throws {
// Given a deferred fulfilment on a value that already matches the expected value.
let initialValue = observable.counter
let deferred = deferFulfillment(observable.observe(\.counter)) { $0 == initialValue }
@@ -22,35 +25,38 @@ class DeferredFulfillmentTests: XCTestCase {
try await deferred.fulfill()
}
func testObservableWithSynchronousUpdate() async throws {
@Test
func observableWithSynchronousUpdate() async throws {
// Given a deferred fulfilment for an expected value.
let newValue = 100
let deferred = deferFulfillment(observable.observe(\.counter)) { $0 == newValue }
// When that value is changed synchronously.
observable.counter = newValue
XCTAssertEqual(observable.counter, newValue)
#expect(observable.counter == newValue)
// Then the test should be fulfilled.
try await deferred.fulfill()
XCTAssertEqual(observable.counter, newValue)
#expect(observable.counter == newValue)
}
func testObservableAsynchronousUpdate() async throws {
@Test
func observableAsynchronousUpdate() async throws {
// Given a deferred fulfilment for an expected value.
let newValue = 100
let deferred = deferFulfillment(observable.observe(\.counter)) { $0 == newValue }
// When that value is changed asynchronously.
Task { try await observable.setCounter(newValue, delay: .seconds(1)) }
XCTAssertEqual(observable.counter, 0)
#expect(observable.counter == 0)
// Then the test should be fulfilled once the update has taken place.
try await deferred.fulfill()
XCTAssertEqual(observable.counter, newValue)
#expect(observable.counter == newValue)
}
func testObservableMultipleUpdates() async throws {
@Test
func observableMultipleUpdates() async throws {
// Given a deferred fulfilment for an expected value.
let finalValue = 500
let deferred = deferFulfillment(observable.observe(\.counter)) { $0 == finalValue }
@@ -61,11 +67,11 @@ class DeferredFulfillmentTests: XCTestCase {
try await observable.setCounter(250, delay: .seconds(.random(in: 1.0...2.0)))
try await observable.setCounter(finalValue, delay: .seconds(.random(in: 1.0...2.0)))
}
XCTAssertEqual(observable.counter, 0)
#expect(observable.counter == 0)
// Then the test should be fulfilled once the expected update has taken place.
try await deferred.fulfill()
XCTAssertEqual(observable.counter, finalValue)
#expect(observable.counter == finalValue)
}
}