apollographql / react-apollo

:recycle: React integration for Apollo Client
https://www.apollographql.com/docs/react/
MIT License
6.85k stars 786 forks source link

Add support for auto-mocking in @apollo/react-testing #3907

Open MrTibbles opened 4 years ago

MrTibbles commented 4 years ago

Having used Apollo for years in production and written countless MockResponses i am all too familiar with the object signature and the creation of those objects. However on occasion it can be tiresome having to create these objects, and more importantly maintain them.

So what i am wondering is that would be it possible to support the exact same behaviour from Apollo Server mocking feature on the MockedProvider test utility? When running tests on the client side it would be possible to enable auto-mocking of network responses to remove the absolute requirement of having to create MockResponse objects for all GQL operations? Considering that the @apollo/react-testing package is a dev dep' it would not pollute the run time bundle size of the overall application. However that is based on limited understanding/thought on where this could/would be implemented. I would like to think that it could be achieved via a prop on <MockedProvider />, using the same implementation from Apollo server with a prop of mocks being a boolean or a custom mock object.

As ever, more than happy to contribute to adding this behaviour, thought i would get a sense of community opinion first.

dylanwulf commented 4 years ago

It is already possible to use the Apollo Server mocking in the client side tests, I write all my client-side tests that way. Instead of using MockedProvider, you can use SchemaLink. This unfortunately means that you will have to keep a copy of your server schema with your client tests (or download the server schema every time the tests run), but I think it's worth it. See this article for more details: https://medium.com/free-code-camp/a-new-approach-to-mocking-graphql-data-1ef49de3d491

MrTibbles commented 4 years ago

That is neat, thanks for sharing link to article.

MrTibbles commented 4 years ago

Leaving open for core contributors visibility. Having a simpler DX as i outline would be excellent, the process @dylanwulf references is neat and achievable, but not as seamless as the Apollo Server mocking feature i referenced.