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.

TLDR: There are Two Keys to Getting this Right

The TLDR is that there are two keys to getting this right:

  1. Using the MockedProvider from the @apollo/client/testing library to wrap our component in the test
  2. Providing mocks that exactly match the query you’re trying to mock

#apollo #graphql #testing #react #javascript

Mocking Apollo GraphQL Queries in React Testing
2.20 GEEK