Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get rid of MainActor warning on all "it" tests? #1180

Closed
Kalzem opened this issue Nov 6, 2022 · 4 comments
Closed

How to get rid of MainActor warning on all "it" tests? #1180

Kalzem opened this issue Nov 6, 2022 · 4 comments

Comments

@Kalzem
Copy link

Kalzem commented Nov 6, 2022

What did you do?

Using Quick v6, I had to add a lot of @mainactor to it("...") tests such as

class TutorialManagerSpec: QuickSpec {
    override func spec() {
        describe("foo") {
            it("can do bar") { @MainActor in // <- Xcode Warning
                // Some test
            }
        }
    }
}

That's because of the architecture of the project which is based on singletons which must run on the main thread at all costs.

This generates an xcode warning for every test (so there are hundreds and hundreds of warnings).

Converting function value of type '@ MainActor () -> ()' to '() throws -> Void' loses global actor 'MainActor'

Is there a specific syntax to get rid of this warning?

Environment

List the software versions you're using:

  • Quick: 6.0.0
  • Nimble: 11.1.0
  • Xcode Version: 14.0.1
  • Swift Version: The one of 14.0.1
  • CocoaPods: 1.11.3
@younata
Copy link
Member

younata commented Nov 6, 2022

That's odd. You shouldn't be getting those warnings with Quick 6. It might be some weird caching behavior? I'll look into it some more later.


Regardless, to improve your developer experience: Since you're compiling with swift 5.7 (Xcode 14), you can annotate the entire class as @MainActor, and your tests will all run on the main actor without needing tag each individual test with @MainActor.

That is, instead of writing the following:

class MySpec: QuickSpec {
    override func spec() {
        it("") { @MainActor in
            expect(Thread.isMainThread).to(beTrue())
        }
    }
}

You can instead write:

@MainActor
class MySpec: QuickSpec {
    override func spec() {
        it("runs on the main actor") {
            expect(Thread.isMainThread).to(beTrue())
        }
    }
}

@slava-semeniuk-ek
Copy link

slava-semeniuk-ek commented Nov 16, 2022

Adding MainActor to the spec class doesn't work for me as well =(

It seems it doesn't work, since originally func spec() is not MainActor at QuickSpec class

@bvirlet
Copy link
Contributor

bvirlet commented Dec 21, 2022

Hello,

Is there a recommend solution for this, or should func spec() be MainActor in QuickSpec?

Thank you!

@younata
Copy link
Member

younata commented Jul 1, 2023

This should be resolved as of Quick 7. Please re-open if it isn't.

@younata younata closed this as completed Jul 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants