Search⌘ K
AI Features

Testing Custom React Hooks

Explore how to effectively test custom React hooks in your applications. This lesson guides you through using React Testing Library and the renderHook utility to create clear, maintainable tests that verify your hooks' behavior, such as updating the document title.

Starter project

The project for this lesson contains a HomePage component that uses a custom hook, useTitle, to set the document title to "My App - Home". In this lesson, we’ll create a test for useTitle.

A copy of the project is in the code widget below. Clicking the “Run” button executes the tests.

export default "test-file-stub";
Incomplete code

First attempt

Create a test file adjacent to useTitle.js in the src/common folder called useTitle.test.js. Add the following test to useTitle.test.js: ...