Rendering a React component for testing

Jest is based upon Node.js, meaning that it can't use the DOM to render your component and test its functionality. Therefore, you need to add a React core package to your project, which can help you render the component without a DOM. Let's take a look at this here:

  1. From your Terminal, execute the following command, which will install react-test-renderer in your project. It can be installed as a devDependency as you don't need to run tests on the build version of your application:
npm install react-test-renderer --save-dev
  1. With react-test-renderer installed, you can now import this package into the src/components/Header/SubHeader.test.js file. This package returns a method called ShallowRenderer ...

Get React Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.