Testing GraphQL Applications
Learn how to test React components in a GraphQL application.
So far we when we were building applications, we were only focused on writing application code, but how do we ensure that our application works as expected? We need to have automated tests to ensure that it works correctly in different conditions.
This is what we will explore in this lesson! We will see how to implement a few unit tests to check if our application’s home page renders correctly. To do this, we will learn how to emulate GraphQL responses and GraphQL errors in unit tests.
React testing library
Before writing our first test, we need to decide what React testing library are we going to use in our project. We can choose many options, but in this course, we will use the React Testing Library, a simple and popular testing library for React. But the choice is not particularly important since what we will learn will work with other testing libraries as well, and we won’t use any advanced features of this library in this course.
To get started with React Testing Library, we first need to install two additional dependencies:
npm install --save-dev @testing-library/jest-dom @testing-library/react
Now we can start working on our tests, but instead, let’s learn the basics of using React Testing Library. To do this, we can write a simple test that renders a React component and checks if it was rendered correctly.
To render a React component, we will use the render
function and pass a React component to it. In this test, we will render a simple text component from the Material UI library.
Get hands-on with 1200+ tech skills courses.