CodePark/ questions / Hooks, context & providers
START PRACTICINGPRACTICE
Frontend question · Testing › Hooks, context & providers

This test fails with count still 0 and an act warning. What's the fix?

const { result } = renderHook(() => useCounter());
result.current.increment();
expect(result.current.count).toBe(1);
Wrap the update: act(() => result.current.increment())

Wrap the update: act(() => result.current.increment()). Calling the hook's setter directly isn't a user event that RTL can wrap. So you need act to apply the update and re-render before you assert. The tempting destructuring option makes it worse. count would hold the stale value from the first render. The findByText option is wrong because renderHook renders nothing visible, so screen can't find the count.