Lesson 3. Custom hooks and classic async pattern 4. About testing custom hooks
Writing tests for React components is often seen as time-consuming by developers. The slightest modification in the returned JSX can break tests. But with custom hooks, as you extract some behavior from the components, it would be nice to test this behavior, especially if the hook is supposed to be reusable.
Of course, you can test a hook by testing a component using it, but there is a
way to test it independently of any component:
react-hooks-testing-library. Its
documentation is very well written and goes straight to the point; I strongly
encourage you to look at it if you want to write and maintain reusable hooks.
Using this library, you can test your hooks' behavior as you would test any function: send it some parameters, and check that the returned values are the ones you expect. Plus, you can simulate rerenders and check that your side effects are triggered and cleaned up.