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

Apollo 1.1.0 @testable import in Test Mocks compilation error #2933

Closed
SzymonMatysik opened this issue Apr 5, 2023 · 16 comments
Closed

Apollo 1.1.0 @testable import in Test Mocks compilation error #2933

SzymonMatysik opened this issue Apr 5, 2023 · 16 comments
Assignees
Labels

Comments

@SzymonMatysik
Copy link

SzymonMatysik commented Apr 5, 2023

Summary

While compiling project I've expected issue with ApolloAPI in TestMock.swift, second line:
Module 'ApolloAPI' was not compiled for testing.

Dropping to 1.0.7 erases the issue.
I've tried on Xcode 14.2 and 14.3 - same results.

Version

1.1.0

Steps to reproduce the behavior

Upgrade Apollo from 1.0.7 to 1.1.0 and try to compile.

Logs

Module 'ApolloAPI' was not compiled for testing

Anything else?

Screenshot 2023-04-05 at 15 31 39

@SzymonMatysik SzymonMatysik added bug Generally incorrect behavior needs investigation labels Apr 5, 2023
@AnthonyMDev
Copy link
Contributor

Hmmm interesting! Thanks for the report. How are you including Apollo? Are you using SPM in an Xcode Project, or is your project defined with a Package.swift file?

@AnthonyMDev AnthonyMDev self-assigned this Apr 5, 2023
@SzymonMatysik
Copy link
Author

Thanks for quick response and sorry I forgot to mention, I use SPM, directly via packages dependencies under project settings

@AnthonyMDev
Copy link
Contributor

Thanks for the info! I'll look into how to correct this and get back to you ASAP

@AnthonyMDev AnthonyMDev added this to the Patch Releases (1.1.x) milestone Apr 5, 2023
@AnthonyMDev AnthonyMDev changed the title Apollo 1.1.0 problem compiling project Apollo 1.1.0 @testable import in Test Mocks compilation error Apr 5, 2023
@AnthonyMDev
Copy link
Contributor

@SzymonMatysik I'm not able to reproduce this in a unit test target. Are you accidentally linking ApolloTestSupport to your actual application targets? It should only be linked to your unit test targets.

@calvincestari
Copy link
Member

This was fixed in #2937.

@SzymonMatysik
Copy link
Author

@AnthonyMDev You were right about accidentally linking ApolloTestSupport into main app target, nevertheless when I deleted it and added into UnitTest target only the issue persists. Module 'ApolloAPI' was not compiled for testing

I've checked on 1.1.2, still with no luck

@calvincestari
Copy link
Member

@SzymonMatysik are there no other warnings or errors in the build log? Are you able to replicate this in a separate project that could be shared with us?

@SzymonMatysik
Copy link
Author

@calvincestari I'm trying to get working reproducible project case but as far without luck - everything works on new project.

On the other hand I got some insights on big project - I am able to compile and run the app with success but the Unit Tests got same error as mentioned above.

In this project we got some modules, for example networking layer and models layer, each layer imports Apollo or ApolloAPI and I import those modules into tests as @testable import. Maybe I'm doing something wrong here?

@Tulleb
Copy link

Tulleb commented Apr 20, 2023

In our case, we were using Mocks into our internal build release. Looks like we can't release this target anymore because of the @testable 😵

@wiedem
Copy link

wiedem commented Apr 21, 2023

We've got the same issue and it is not solved with #2937

The reason in our case is that Apollo is a transient dependency of another Swift package (a dependency of a dependency).
In this case testable Imports don't seem to work and you get the error message

Module 'ApolloAPI' was not compiled for testing

@calvincestari
Copy link
Member

calvincestari commented Apr 21, 2023

The reason in our case is that Apollo is a transient dependency of another Swift package (a dependency of a dependency).
In this case testable Imports don't seem to work and you get the error message

This is a good pointer in the right direction - thank you!

@SzymonMatysik, @Tulleb - is there anything similar in the way you're linking to the Apollo library?

@wiedem
Copy link

wiedem commented May 5, 2023

@calvincestari what's the reason this issue was closed?

It seems I've found one cause for the issue:
In our case we're using a separate build configuration for the test target.

BuildConfiguration

Switching to the Debug configuration gets rid of the build error but is not really an option for us since we need a separate build configuration for tests.

I can also reproduce this 100%, when I switch back to the testing configuration, the error occurs again:
TestMock

I have also tried it by simply cloning the debug configuration and thus using an identical configuration with a different name. This alone is enough for this error to occur.

@wiedem
Copy link

wiedem commented May 5, 2023

The root cause of the problem seems to be this:
Update SPM to support custom configuration names

When a Swift package is compiled there are only two configurations that are used: Debug or Release. If a configuration name is unknown, Release is used as a fallback.

So if you use a different build configuration in Xcode, it seems the Apollo targets are all built with a release configuration. Which should ultimately lead to the described problem.

@wiedem
Copy link

wiedem commented May 5, 2023

And to leave a solution here that might help others:
In our case, renaming the configuration from "Testing" to "Debug_Testing" solved the issue.

@calvincestari
Copy link
Member

@calvincestari what's the reason this issue was closed?

It was closed because it was believed to have been fixed - #2933 (comment).

@testable is not usable in anything other than Debug builds. or those specifically marked for 'testing'; Release builds violate both of those and it seems there are some deficiencies in SPM that allows for custom build configurations that would resolve this.

It looks like we can probably remove @testable from the ApolloAPI import but not from the Apollo import because of the need for the internal scoped GraphQLExecutor class. Let's also not forget the intention behind ApolloTestSupport which is for testing; I like that Release builds are prevented from shipping code that should never make it into production. The shortfall in SPM build configurations is an annoying limitation but not something I want to necessarily work around.

To be frank, with the introduction of selection set initializers in 1.1 the future of test mocks is uncertain. Being schema-based they offer a different type of testing ability but the value is diluted now.

And to leave a solution here that might help others:
In our case, renaming the configuration from "Testing" to "Debug_Testing" solved the issue.

I haven't tried this myself but thank you for sharing this to help others that might be facing the same issue.

@wiedem
Copy link

wiedem commented May 5, 2023

@testable is not usable in anything other than Debug builds. or those specifically marked for 'testing';

Just to be clear, our testing configuration was not a release configuration, but a debug configuration. The problem of the SPM arose solely from the configuration name.

That @testable should not work for release builds is out of question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants