Description
Motivation
Confirmation is the closest thing Swift Testing has to the old Expectation API that XCTest had.
The only issue with it, is that it does not wait for its confirmation requirements to be finished. It just executes to the end of its block immediately.
here is an example of this limitation:
// This will fail the test case
await confirmation { confirmation in
myClass.synchronousFuncWithCompletionHandler {
confirmation()
}
}
If you want to make that test pass, you have to add your own artificial delay:
// This will succeed assuming the closure completed within 100ms
try await confirmation { confirmation in
myClass.synchronousFuncWithCompletionHandler {
confirmation()
}
try await Task.sleep(for: .milliseconds(100))
}
This isn't ideal, as it not only delays the execution of your test, but it also adds room for flakiness.
Proposed solution
add a timeout param to the confirmation
function.
The await on the confirmation should end once the expectedCount
is met, or the timeout is reached. Whichever comes first.
example usage:
// this will succeed if the completion handler finishes within 100ms
await confirmation(timeout: .milliseconds(100)) { confirmation in
myClass.synchronousFuncWithCompletionHandler {
confirmation()
}
}
// this will fail due to timeout
await confirmation(timeout: .milliseconds(100)) { confirmation in
try await Task.sleep(for: .seconds(1))
myClass.synchronousFuncWithCompletionHandler {
confirmation()
}
}
Alternatives considered
Alternatively, to not break the behaviour of confirmation, there could be an expectation
function that behaves very similarly, but with the additional features mentioned above.
Additional information
No response