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

MockedProvider is broken after an update to apollo-client 3.1.3 #6861

Closed
knedev42 opened this issue Aug 19, 2020 · 13 comments
Closed

MockedProvider is broken after an update to apollo-client 3.1.3 #6861

knedev42 opened this issue Aug 19, 2020 · 13 comments

Comments

@knedev42
Copy link

I'm in the process of migrating a large codebase to the newest version of apollo-client. Most of our tests (there are hundreds of those) rely on the MockedProvider and they used to pass with apollo-client 2.x.x.

I debugged some of them and they all exhibit the same weird behavior - the cache gets populated with the mocked response, but the data and error properties of the query are undefined. The loading state correctly changes from "false" to "true" and then back to "false" after the cache gets populated. The network status goes from 1 (loading) to 7 (meaning the query is completed without errors). However both the data and error properties of the useQuery hook remain undefined...
I don't know if it matters, but I am using the skip property and it correctly fires off the query when it switches from true to false (after all the cache also gets populated with the mocked data). The fetch policy I'm using is cache-and-network with a nextFetchPolicy: 'cache-first'. Also the tests for error states are passing (the ones with a mock that returns an error instead of data). The application does work outside of the tests, and that's why I suspect it's related to the MockedProvider. Also these tests used to pass 🤷

Intended outcome:

To have either the data or error populated with some value after a query is completed.

Actual outcome:

Both data and error are undefined after the query is completed.

Versions

System:
OS: Windows 10 10.0.18363
Binaries:
Node: 10.15.1
Yarn: 1.19.0
npm: 6.4.1
Browsers:
Chrome: 84.0.4147.125
Edge: Spartan (44.18362.449.0)
npmPackages:
@apollo/client: ^3.1.3 => 3.1.3

@knedev42 knedev42 changed the title Tests (MockedProvider) broke after an update to apollo-client 3.1.3 MockedProvider is broken after an update to apollo-client 3.1.3 Aug 19, 2020
@knedev42
Copy link
Author

knedev42 commented Aug 19, 2020

I dug a bit more into this and it turns out that some of the mocks have missing fields (which are irrelevant for the particular test).
These break the MockedProvider and even though it fills up the cache, it doesn't set the data to the mocked response, but instead keeps it as undefined.

If I change the mocks to include all the fields requested by the query it starts working and the data property is populated.
If I miss even a single field it becomes undefined... That's quite a big breaking change :(

It can be really tedious to fill up all the mocks with the missing irrelevant fields, since some of our queries return lists and have dozens of fields on each item (and lots of nesting). Is there a reason why this behavior would change from apollo-client v2? It was very convenient to be able to return a mock with only the data we need for a particular test, and not have to write fixtures with the entire complex objects.

@tyagow
Copy link

tyagow commented Aug 19, 2020

Duplicated of #6803

@knedev42
Copy link
Author

@tyagow It's not really a duplicate though, #6803 is about a different problem.

@ivaylopetrovdev
Copy link

This is so weird... I can confirm @knedev42's observation. If mocked response is not matching exactly the GraphQL, then the returned data is always undefined. This is really unpleasant way to MOCK. It's a MOCK after all! Please review and fix this issue!

@Jiert
Copy link

Jiert commented Sep 14, 2020

I ran into this as well when trying to upgrade from 3.0.0-beta.43 to 3.1.4.

@marcusorjames
Copy link

This is so weird... I can confirm @knedev42's observation. If mocked response is not matching exactly the GraphQL, then the returned data is always undefined. This is really unpleasant way to MOCK. It's a MOCK after all! Please review and fix this issue!

This was exactly my issue. Thank you, my mock data wasn't in the same shape as the query, infuriating! Thanks for the assist.

@ireznik
Copy link

ireznik commented Oct 22, 2020

Is there any plan to fix this issue? i understand that some bugs are not that major, but mocking a mock which is provided and currently not usable for 2 months is somehow scary for a library which is used widely in the wild. I am actually quite disappointed

Edit: okay after upgrading to the mentioned beta, it actually works as intended

@3nvi
Copy link

3nvi commented Nov 26, 2020

I dug a bit more into this and it turns out that some of the mocks have missing fields (which are irrelevant for the particular test).
These break the MockedProvider and even though it fills up the cache, it doesn't set the data to the mocked response, but instead keeps it as undefined.

If I change the mocks to include all the fields requested by the query it starts working and the data property is populated.
If I miss even a single field it becomes undefined... That's quite a big breaking change :(

It can be really tedious to fill up all the mocks with the missing irrelevant fields, since some of our queries return lists and have dozens of fields on each item (and lots of nesting). Is there a reason why this behavior would change from apollo-client v2? It was very convenient to be able to return a mock with only the data we need for a particular test, and not have to write fixtures with the entire complex objects.

Thanks for that. Spent 3 hours today trying to debug this. Hope I found this issue earlier.

@christopherread
Copy link

Confirmed I had the same problem with undefined data being returned from MockedProvider using @apollo/client: ^3.2.5, and filling in all the fields on mocked data result fixed it.

@AdrienLemaire
Copy link

I got bitten by this issue as well. If matching exactly the graphql field matters for tests, please at least throw an error instead of returning undefined.

@essaji
Copy link

essaji commented Feb 8, 2021

For me it is happening because of using skip, if I remove skip the test passes but in the actual world I need the skip property, looking for some workaround to at leas pass the test for now

@hwillson
Copy link
Member

hwillson commented May 5, 2021

Let us know if this is still a concern with @apollo/client@latest - thanks!

@hwillson hwillson closed this as completed May 5, 2021
@jmvtrinidad
Copy link

This is great. Now working in version 3.4.8
It is now returning the data even there is a missing field and it will also log the missing field in the console.
Thank you

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests