Loading

We’ve got a custom useCounter hook here and we want to make sure the increment and decrement functions it returns will update the count state that it returns. Because hooks must be run within the render phase of a component, we’ll create a simple component that simply uses the hook and render that component. One catch is that whenever there’s a state update in your tests, React needs you to wrap that in a call to act. We don’t have to do that for regular component tests that use React Testing Library utilities because those utilities manage the act call internally, but since we’re calling these state updater functions ourselves, we have to wrap things in act. Let’s do that for our useCounter hook.

Transcript

Test a Custom React Hook with React’s Act Utility and a Test Component