React Hooks Testing Library

Examine the usage for the React Hooks Testing Library and when it should be used.

The benefits and an example

react-hooks-testing-library is an open-source npm package that tests React Hooks.

The Testing Library makes it easier to interact with a hook during unit testing. Some benefits are mentioned below:

  • Renders the hook using renderHook helper and provides the result

  • Allows you to access properties from hook output by using result.current. This reference is set to the output of the currently rendered hook.

  • Provides act helper which can be used to trigger functions returned by hook. act automatically re-renders the hook and sets the result.current reference accordingly.

  • Allows testing of the React hooks independently of React components. This is useful for testing complex business logic defined in a hook and it covers all possible scenarios.

The following example shows how to use the Testing Library to test the example from the previous lesson.

Get hands-on with 1200+ tech skills courses.