Testing React with Enzyme
So far, we've managed to test our server and all GraphQL API functions. Currently, however, we're still missing the tests for our frontend code. While we render the React code when requesting any server route, such as the /app
path, we only have access to the final result and not to each component. We should change this to execute the functions of certain components that aren't testable through the backend. First, install some dependencies before using npm
, as follows:
npm install --save-dev enzyme @wojtekmaj/enzyme-adapter-react-17ignore-styles jsdom isomorphic-fetch
The various packages are described in more detail here:
- The
enzyme
and@wojtekmaj/enzyme-adapter-react-17
packages provide React with specific features to render and interact with the React tree. This can be through either a real Document Object Model (DOM) or shallow rendering. We are going to use a real DOM in this chapter because it allows us to test all features...