Is there some bug in Swift Testing?
This runs 100 times without any failure. It’s not testing anything, but it just works.
@MainActor @Test
func doesHandleResultCorrectly() async {
let manager = Mock()
let (view,vm) = getView(manager: manager)
view.handleSuccess(of: .owned)
print(vm.wasHandledCalled)
}
while this hangs for 10-15 seconds and then crashes:
@MainActor @Test
func doesHandleResultCorrectly() async {
let manager = Mock()
let (view,vm) = getView(manager: manager)
view.handleSuccess(of: .owned)
#expect(vm.wasHandledCalled) // ERROR: Thread 1: EXC_BAD_ACCESS (code=2, address=0x16ac0bdf8)
}
The crash expands the macros as such
Testing.__checkPropertyAccess(vm.self, getting: { $0.wasHandledCalled }, expression: .__fromPropertyAccess(.__fromSyntaxNode("vm"), .__fromSyntaxNode("wasHandledCalled")), comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()
I have tried a number of things and don't think this is a threading issue. I also used Thread Sanitizer but nothing pops up.
I was using Xcode 16.1 and it has a bug. The bug is reported here:
The macro can't properly handle cases where the expression evaluates to false
and it just then crashes!
Using an explicit ==
helped the macro do things correctly.
#expect(vm.wasHandledCalled == true) // would fail my test, but not crash
#expect(vm.wasHandledCalled == false) // would pass my test
#expect(vm.wasHandledCalled) // would just crash instead of failing
To be clear I didn't want my test to fail, but was in the middle of writing my tests and with it crashing I couldn't realize that my logic was incorrect.
As of July 2, 2025 there hasn't been a fix out yet. There's a branch to fix it
It’s not merged yet because it causes some problems with the type checker