When testing our frontend code, we might find ourselves needing to mock GraphQL query responses. Here’s how to do it.
When I learn a new technology, the first thing I think is “okay, how do I use this?” But soon thereafter, the question becomes “how do I test this?”
That was the case recently with Apollo GraphQL in my React project. In this post, we’ll learn how to test a component that uses a GraphQL query.
The TLDR is that there are two keys to getting this right:
MockedProvider from the
@apollo/client/testing library to wrap our component in the test
Learn how to test a component that uses Apollo GraphQL Queries in React Testing. Using the MockedProvider from the @apollo/client/testing library to wrap our component in the test. Providing mocks that exactly match the query you’re trying to mock