React · Senior · Question 70 of 75
How do you unit-test a custom hook?
const { result } = renderHook(() => useCounter(5));
act(() => result.current.increment());
expect(result.current.count).toBe(6);- ✓Correct answer: Render it with
renderHookfrom Testing Library and wrap updates inact - 2Call the hook directly in the test
- 3Hooks can only be tested through end-to-end tests
Explanation
Hooks need a component context. Use the wrapper option to provide context providers (router, query client) the hook depends on.