Let's now write tests for the useCounter Hook we created, by following these steps:
- Create a new src/hooks/useCounter.test.js file.
- Import the renderHook and act functions from the React Hooks Testing Library, as we are going to use these later:
import { renderHook, act } from '@testing-library/react-hooks'
- Also, import the to-be-tested useCounter Hook, as shown here:
import useCounter from './useCounter'
- Now we can write our first test. To define a test, we use the test function from Jest. The first argument is the name of the test and the second argument is a function to be run as the test:
test('should use counter', () => {
- In this test, we use the renderHook function to define our Hook. This function ...