ApprovalTests.Swift icon indicating copy to clipboard operation
ApprovalTests.Swift copied to clipboard

Fail to get test function name with `test() async throws`

Open ytyubox opened this issue 2 years ago • 2 comments

Hi, Thank you a lot for having this project; I love it.

I found an ERROR when using verify()

With the following test update, you can see the file name become *.ERROR.*

The approved file name is ApprovalTests_SwiftTests/Namer/NamerTests.ERROR.story1.approved.md

diff --git a/ApprovalTests_SwiftTests/Namer/NamerTests.ERROR.story1.approved.md b/ApprovalTests_SwiftTests/Namer/NamerTests.ERROR.story1.approved.md
new file mode 100644
index 0000000..94f111a
--- /dev/null
+++ b/ApprovalTests_SwiftTests/Namer/NamerTests.ERROR.story1.approved.md
@@ -0,0 +1,2 @@
+# Title
+> Quoted text
diff --git a/ApprovalTests_SwiftTests/Namer/NamerTests.swift b/ApprovalTests_SwiftTests/Namer/NamerTests.swift
index fc5171e..d56e0d7 100644
--- a/ApprovalTests_SwiftTests/Namer/NamerTests.swift
+++ b/ApprovalTests_SwiftTests/Namer/NamerTests.swift
@@ -9,6 +9,9 @@ final class NamerTests: XCTestCase {
     func test_parameters() throws {
         try Approvals.verify("# Title\n> Quoted text", Approvals.Names.withParameters("story1").forFile.with(extensionWithDot: ".md"))
     }
+    func test_parameters_async() async throws {
+        try Approvals.verify("# Title\n> Quoted text", Approvals.Names.withParameters("story1").forFile.with(extensionWithDot:  ".md"))
+    }
 
     func test_multipleParameters() throws {
         let foo = Approvals.Names.withParameters("story1")

I would love to contribute, but I am confused about why check the process for the test function name. There is a way to inject function seamlessly, just like file and line.

diff --git a/ApprovalTests.Swift/Approvals.swift b/ApprovalTests.Swift/Approvals.swift
index bdc75bf..5ab5978 100644
--- a/ApprovalTests.Swift/Approvals.swift
+++ b/ApprovalTests.Swift/Approvals.swift
@@ -29,6 +29,7 @@ public enum Approvals {
         _ object: T,
         _ options: Options = Options(),
         file: StaticString = #filePath,
+        function: StaticString = #function,
         line: UInt = #line
     ) throws {
         let description = String(describing: object)

ytyubox avatar Feb 01 '23 09:02 ytyubox

Thank you! This is important, now that we can have async tests.

jonreid avatar Feb 12 '23 01:02 jonreid

Hi @jonreid, could you take a look at the PR? #17

ytyubox avatar Mar 30 '23 08:03 ytyubox

This is an important PR for me. Without this PR, all async tests in a test class generate the same filename of [TestClassName].ERROR.approved.txt so different tests are trying to verify against the same source. This obviously doesn't work.

Seems like a straightforward and it would be good to see it merged. :)

babbage avatar May 01 '24 01:05 babbage