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);

Explanation

Hooks need a component context. Use the wrapper option to provide context providers (router, query client) the hook depends on.

Further reading

Problem with a question

React · Senior · question 70

Test yourself

Take the React · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz