A Comprehensive Guide to Testing React Hooks

Boaz Hwang
4 min readJan 6, 2023

--

As a senior frontend engineer, you know the importance of testing in any software project. Properly testing React Hooks is crucial to ensuring that your components are working correctly and preventing bugs in the future. In this blog post, we’ll go over the various approaches you can take to test React Hooks, as well as best practices for setting up a testing workflow.

There are three main approaches to testing React Hooks:

  1. Manually testing
  2. Using a testing library like Enzyme
  3. Using React’s own testing utilities

Let’s dive into each of these approaches in more detail.

Manually testing

One approach to testing Hooks is to manually test them by rendering the component that uses the Hook and checking its output. This can be a simple way to test basic Hooks, but it can be time-consuming for more complex components. To manually test a Hook, you can use React’s act function to wrap the code that calls the Hook, and then use standard DOM manipulation methods to check the component's output.

For example, consider the following component that uses the useState Hook to toggle a boolean value:

function ToggleButton() {
const [isOn, setIsOn] = useState(false);

return (
<button onClick={() => setIsOn(!isOn)}>
{isOn ? 'On' : 'Off'}
</button>
);
}

To manually test this component, you can use act and render from the react-testing-library package:

import { act, render } from 'react-testing-library';

test('ToggleButton toggles value when clicked', () => {
const { getByText } = render(<ToggleButton />);

// Initial value should be "Off"
expect(getByText('Off').textContent).toBe('Off');

// Click the button
act(() => {
getByText('Off').click();
});

// Value should now be "On"
expect(getByText('On').textContent).toBe('On');
});

This test will render the ToggleButton component, click the button, and verify that the text content changes from "Off" to "On".

Using a testing library like Enzyme

Another approach to testing Hooks is to use a testing library like Enzyme. Enzyme is a popular testing library that provides several utility functions for rendering and manipulating React components. With Enzyme, you can easily test the output of a component that uses Hooks, as well as the effects of calling the Hooks.

To use Enzyme to test a component that uses Hooks, you can use the mount or shallow function to render the component, and then use Enzyme's utility functions to manipulate and test the component.

For example, consider the following test for the ToggleButton component using Enzyme's mount function:

import { mount } from 'enzyme';

test('ToggleButton toggles value when clicked', () => {
const wrapper = mount(<ToggleButton />);

// Initial value should be "Off"
expect(wrapper.text()).toBe('Off');

// Click the button
wrapper.find('button').simulate('click');

// Value should now be "On"
expect(wrapper.text()).toBe('On');
});

This test will render the ToggleButton component using Enzyme's mount function, click the button using simulate, and verify that the text content changes from "Off" to "On".

Using React’s own testing utilities

React also provides its own testing utilities that can be used to test components that use Hooks. These utilities include act, which allows you to test effects caused by Hooks, and renderHook, which allows you to test Hooks in isolation.

act is a function that allows you to wrap the code that calls a Hook, similar to the way it's used in manually testing. The difference is that act is designed specifically for testing, and it ensures that all updates caused by the Hook are properly flushed before the test is finished.

renderHook is a utility function that allows you to test a Hook in isolation. It takes a function that calls the Hook, and returns an object with the Hook's values and helper functions. This can be useful for testing custom Hooks or for testing the behavior of a Hook in different scenarios.

For example, consider the following test for the useToggle custom Hook using renderHook:

import { renderHook } from '@testing-library/react-hooks';

function useToggle(initialValue = false) {
const [value, setValue] = useState(initialValue);

function toggle() {
setValue(!value);
}

return [value, toggle];
}

test('useToggle toggles value', () => {
const { result } = renderHook(() => useToggle());
const [value, toggle] = result.current;

// Initial value should be false
expect(value).toBe(false);

// Toggle the value
act(() => {
toggle();
});

// Value should now be true
expect(result.current[0]).toBe(true);
});

This test will render the useToggle Hook, toggle the value, and verify that the value changes from false to true.

Best practices for testing React Hooks

No matter which approach you choose for testing Hooks, there are a few best practices that can make your testing workflow more efficient and effective:

  • Write tests for all the Hooks you use. It’s important to test not just your own custom Hooks, but also the built-in Hooks like useState and useEffect. This will help ensure that all the Hooks in your application are working correctly.
  • Use a test runner like Jest to automate your tests. A test runner like Jest can make it easier to run your tests and see the results. It can also help you catch errors and failures early on in the development process.
  • Set up a testing workflow as part of your development process. Make testing a regular part of your development workflow by writing tests as you develop your components, and running your tests regularly to catch any issues early on.

By following these best practices, you can ensure that your React Hooks are properly tested and working correctly.

--

--