Jest provides a handful of functions that can be used to test React components. The list can be found at https://jestjs.io/docs/en/snapshot-testing.
Let's consider a React component, as shown in the following code and in the file Header.js
:
import React from "react"; import Link from "react-router-dom/Link"; const Header = () => ( <ul> <li> <Link to="/">Home</Link> </li> <li> <Link to="/about">Services</Link> </li> <li> <Link to="/topics">Contact Us</Link> </li> <li> <Link to="/topics">Login</Link> </li> </ul> ); export default Header;
Testing the component with Jest is simple. We import the component and use snapshot testing:
import React from "react"; import renderer from "react-test-renderer"; import Header from "../Header"; jest.mock("react-router-dom/Link", () => "Link...