I just realized I've been putting the `try` in the wrong place in Swift Testing the whole time 😩 It should be:

try #expect(result == expected)

not:

#expect(try result == expected)

The stupid thing is that I *knew* about this, and I was careful to be consistent, but I was consistently *wrong*

Try #expect(throwing()) or #expect(try throwing())?

try #expect(throwing()) or #expect(try throwing())? It seems like these are often interchangeable… but I am also seeing cases where only try #expect(throwing()) compiles without errors (and #expect(try throwing()) does error). Is there a best practice between these two? A related question is async expectations: await #expect(awaiting()) or #expect(await awaiting())? Is it generally a better idea (less probability for a compiler error) to specify the await outside of the expect?

Swift Forums

@nicklockwood there is an open issue where `expect try` might be blocked. the workaround for now is to split it across two lines. which afaik is still recommended instead of `try expect`.

https://github.com/swiftlang/swift/issues/75256

@vanvoorden @nicklockwood

let result = try …
#expect(result == expected)

This is the way

@danielkasaj @vanvoorden how does the output differ in this approach vs putting the try before the #expect?
@nicklockwood @vanvoorden you don’t have to worry if it’s before #expect or inside the parentheses
@danielkasaj @nicklockwood @vanvoorden That wasn't my question
@nicklockwood @vanvoorden I’m sorry but I don’t understand your issue. Kindly spell it out for me. (No sarcasm whatsoever)
@danielkasaj @nicklockwood @vanvoorden I was trying to understand why this is better than just putting the try before the #expect() as I am doing. I understand now though - the issue is that putting try outside the expect sometimes won't compile (in my case it does, so I think it doesn't matter in this case)