apollo-client: Can't test loading state using MockedProvider
Intended outcome: I tried to test loading state
import {act, cleanup, render} from 'react-native-testing-library';
import { MockedProvider } from '@apollo/client/testing';
const {getByTestId} = render(
<MockedProvider mocks={[]} addTypename={false}>
{<ProductsScreen {...props} />}
);
await act(() => wait(0));
expect(getByTestId('ActivityIndicator')).not.toBe(null);
Actual outcome: but it gives me an error No more mocked responses for the query
Versions System: OS: macOS 10.15.3 Binaries: Node: 10.18.1 Yarn: 1.21.1 npm: 6.13.6 Browsers: Chrome: 83.0.4103.97 Safari: 13.0.5 npmPackages: @apollo/client: ^3.0.0-rc.0 => 3.0.0-rc.0
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 22
- Comments: 23 (4 by maintainers)
I found a super simple solution to the problem. Just use a very high delay value for the mocked response. This keeps the query indefinitely long in the loading state.
Example:
This behavior is even suggested in the documentation, though of course also giving the same Error: https://www.apollographql.com/docs/react/development-testing/testing/#testing-loading-states
Same here, hundreds of our tests failed because of this issue. Please bring back the old behavior.
+1
I have same issue on @apollo/client 3.0.1 version
this test code is failed. and error message
Hitting this while upgrading my project, it would be nice if there was a MockProvider mode that operated in a less strict mode, as it did prior to the update.
I have a workaround that appears to be working for me at the moment on
@apollo/client v3.0.2
.Using the OPs code as a starting point (NOT WORKING):
I recommend three changes:
No more mocked responses
error.MockedProvider
will update state after our expectation. To keep our test clean of Reactact()
warnings, we need to wrap that state change in act.All together, that looks like this (WORKAROUND SOLUTION):
So those shenanigans get my Apollo-loading tests passing stably in any order again. š But it would be much nicer if a mock of
[]
would not throw aNo more mocked responses
error to begin with. Then there would be no state change to worry about wrapping inact
, and no need to sneak in an expectation before the next tick, and testing the loading state would be nice and straightforward again. šSame problem here⦠Trying to update to
@apollo/client
and got tens of these āNo more mocked responsesā errors. Also in the simple loading state check.I am attempting to upgrade to
@apollo/client
with todayās new release and having the same issue with tests for loading states.FWIW, In my opinion, when
MockedProvider
receivesmocks={[]}
, it should detect this as the intentional āloadingā input, and just return null without throwing an error. That will keep the provider in the loading state indefinitely, and will save users the current headaches:I believe the way to go is to add something like this at line 76 of
mockLink.ts
(just a guess. Iām not sufficiently knowledgeable about the codebase):This creates separation between cases where we want the error (a requested key does not exist in the mock) and when we do not want the error (no keys exist in the mock).
For anyone still experiencing this bug (I am, even with everything up to date) I came up with an interim fix that doesnāt patch anything.
Add a new
LoadingApolloLink.js
somewhere in your project:Now use this link in your provider: Note: I havenāt actually tried this out in tests, only Storybook
My story parameter setup looks like this: