-
Notifications
You must be signed in to change notification settings - Fork 36
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
Help understanding this error - "Error: ApolloNextAppProvider cannot be used outside of the Next App Router!" in unit tests #283
Comments
Thank you for reporting this! You're right, this is a check that we added newly, and it's probably a bit too strict. I'm changing it over in #284 so that the SSR build of Jest should automatically pick up the browser build, but I believe Vite will set you up with the SSR build per default. You can see this Keep in mind that you also call the cleanup function after each test: import { resetNextSSRApolloSingletons } from "@apollo/experimental-nextjs-app-support/ssr";
afterEach(resetNextSSRApolloSingletons); Could you please try the PR release from that PR and report back if that does the trick for you? npm i @apollo/[email protected] |
Good morning @phryneas. Thanks for your speedy response! I still have issues after updating the version to I previously added I did not have two of those settings in my Here's an article that kinda explains our implementation (if that is helpful). Note: We don't have the client side The gist of the implementation is here in setupTests. The issue I think is that worker is actually coming from a node package.
I'll try to adjust my implementation to see if I can get the serviceWorker to work instead for tests. I'll have to check back later for that Thanks! ------------ Context ----------- Here's context of what I have tried — if I just update the package version without modifying Vitest I see errors like the one below. I think this eludes to needed the browser build since next navigation imports client side hooks.
If I add
Then when I add
For extra context here is my vitetest.config:
|
This is problematic :/
I'll see if I can set up a chat with @kettanaito to talk this through. |
@phryneas, I will be glad to chat about this. Conditional exports have caused a lot of pain to the community but I am convinced MSW describes them correctly. Multi-environment tooling is still hard.
I can propose a counter-argument: using a Even if we provide a dummy entrypoint like so: {
"exports": {
"/node": { "browser": "./dummy.js" }
}
} // dummy.js
throw new Error('Only use this in Node.js!') It won't help, only obscure the mistake and make it harder to spot. The problem here is the I have a few thoughts on this. First, code meant for the browser must be tested in the browser. JSDOM has to go, I'm sorry. It's a broken tool by design and there's no reason not to use Playwright or Cypress nowadays. Second, if you absolutely must load a particular build of your app or a third-party, rather prefer using export default defineConfig({
resolve: {
alias: {
'dep-a': '/node_modules/dep-a/dist/index.browser.js',
'dep-b': '/node_modules/dep-b/dist/index.browser.js',
}
}
}) |
We've had a fantastic discussion with @phryneas and we've found a solution to this. He's working on the pull requests, I will review those and release the fix shortly. Stay tuned. |
See mswjs/msw#2134 and mswjs/interceptors#552 |
Thanks for all this quick collaboration! Is this ready for me to test? I bumped the packages: Running tests gives this error output
|
You will still need I am confused about that Could you please change |
The changes are not ready yet. Wait for the next MSW minor release until trying anything. I need to wrap a few things and then the package will be there. |
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better. |
This has been released as 0.10.1 on our side, but we still need to verify the full story once the msw release is out, so keeping this open :) |
[email protected] has been released, including the fix for this. Please update and let me know! |
@phryneas @kettanaito it looks like this works! Thanks so much for jumping in here. Just for context in case anyone reads this thread, I updated both packages and then added the browser resolve condition to my vitest.config.
|
Also worth mentioning, despite setting |
Since it's working for you now, I'm gonna close the issue :) |
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better. |
Hello!
We've been using
@apollo/experimental-nextjs-app-support
for a few months now with no issue; however, after the update fromv0.8.0
tov.0.9.0+
we have breaking unit tests with this error:Error: ApolloNextAppProvider cannot be used outside of the Next App Router!
This seems to be related to how we test components that require the ApolloWrapper/Provider. For context, we're using Mock Service Worker, Vitest, Next App router. We have a custom renderer function as a testing utility that allows us to wrap components in Providers that might be needed. In many of our cases we need to mount components with the ApolloWrapper so we can mock graphql calls inside our tests. Below I will add the error output that we are seeing. I will also post some code snippets and the versions we're running.
The error we see are these:
The strategy we use for simplifying the testing boilerplate for Wrappers/Providers comes right from the RTL docs for creating a custom renderer. This has worked thus far perfectly. After the version bump, this render function is throwing the error mentioned above.
When inside the unit test file, if I remove this custom render function and replace it with the RTL
render
function and I don't mount ApolloWrapper, my tests will pass, provided that I change the mocking structure of the network calls.In our tests look like this. All the tests that we have that require the ApolloWrapper/Provider fail with the error.
Here's what our ApolloProvider looks like for additional context. The implementation still seems to work locally. We're just having issues with our tests after the version.
Thanks! Let me know if I should add more context.
The text was updated successfully, but these errors were encountered: