react-hooks-testing-library

ram

Simple and complete React hooks testing utilities that encourage good testing practices.


Read The Docs

[![Build Status](https://img.shields.io/github/workflow/status/testing-library/react-hooks-testing-library/validate?logo=github&style=flat-square)](https://github.com/testing-library/react-hooks-testing-library/actions?query=workflow%3Avalidate) [![codecov](https://img.shields.io/codecov/c/github/testing-library/react-hooks-testing-library.svg?style=flat-square)](https://codecov.io/gh/testing-library/react-hooks-testing-library) [![version](https://img.shields.io/npm/v/@testing-library/react-hooks.svg?style=flat-square)](https://www.npmjs.com/package/@testing-library/react-hooks) [![downloads](https://img.shields.io/npm/dm/@testing-library/react-hooks.svg?style=flat-square)](http://www.npmtrends.com/@testing-library/react-hooks) [![MIT License](https://img.shields.io/npm/l/@testing-library/react-hooks.svg?style=flat-square)](https://github.com/testing-library/react-hooks-testing-library/blob/main/LICENSE.md) [![All Contributors](https://img.shields.io/github/all-contributors/testing-library/react-hooks-testing-library?color=orange&style=flat-square)](#contributors) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) [![Code of Conduct](https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square)](https://github.com/testing-library/react-hooks-testing-library/blob/main/CODE_OF_CONDUCT.md) [![Netlify Status](https://api.netlify.com/api/v1/badges/9a8f27a5-df38-4910-a248-4908b1ba29a7/deploy-status)](https://app.netlify.com/sites/react-hooks-testing-library/deploys) [![Discord](https://img.shields.io/discord/723559267868737556.svg?color=7389D8&labelColor=6A7EC2&logo=discord&logoColor=ffffff&style=flat-square)](https://discord.gg/testing-library) [![Watch on GitHub](https://img.shields.io/github/watchers/testing-library/react-hooks-testing-library.svg?style=social)](https://github.com/testing-library/react-hooks-testing-library/watchers) [![Star on GitHub](https://img.shields.io/github/stars/testing-library/react-hooks-testing-library.svg?style=social)](https://github.com/testing-library/react-hooks-testing-library/stargazers) [![Tweet](https://img.shields.io/twitter/url/https/github.com/testing-library/react-hooks-testing-library.svg?style=social)](https://twitter.com/intent/tweet?text=Check%20out%20react-hooks-testing-library%20by%20%40testing-library%20https%3A%2F%2Fgithub.com%2Ftesting-library%2Freact-hooks-testing-library%20%F0%9F%91%8D) ## A Note about React 18 Support If you are using the current version of `react-testing-library`, replace ```js import { renderHook } from '@testing-library/react-hooks' ``` with ```js import { renderHook } from '@testing-library/react' ``` Once replaced, `@testing-library/react-hooks` can be uninstalled. ### Details As part of the changes for React 18, it has been decided that the `renderHook` API provided by this library will instead be included as official additions to both `react-testing-library` ([PR](https://github.com/testing-library/react-testing-library/pull/991)) and `react-native-testing-library` ([PR](https://github.com/callstack/react-native-testing-library/pull/923)) with the intention being to provide a more cohesive and consistent implementation for our users. Please be patient as we finalise these changes in the respective testing libraries. In the mean time you can install `@testing-library/react@^13.1` ## Table of Contents - [The problem](#the-problem) - [The solution](#the-solution) - [When to use this library](#when-to-use-this-library) - [When not to use this library](#when-not-to-use-this-library) - [Example](#example) - [`useCounter.js`](#usecounterjs) - [`useCounter.test.js`](#usecountertestjs) - [Installation](#installation) - [Peer Dependencies](#peer-dependencies) - [API](#api) - [Contributors](#contributors) - [Issues](#issues) - [πŸ› Bugs](#-bugs) - [πŸ’‘ Feature Requests](#-feature-requests) - [❓ Questions](#-questions) - [LICENSE](#license) ## The problem You're writing an awesome custom hook and you want to test it, but as soon as you call it you see the following error: > Invariant Violation: Hooks can only be called inside the body of a function component. You don't really want to write a component solely for testing this hook and have to work out how you were going to trigger all the various ways the hook can be updated, especially given the complexities of how you've wired the whole thing together. ## The solution The `react-hooks-testing-library` allows you to create a simple test harness for React hooks that handles running them within the body of a function component, as well as providing various useful utility functions for updating the inputs and retrieving the outputs of your amazing custom hook. This library aims to provide a testing experience as close as possible to natively using your hook from within a real component. Using this library, you do not have to concern yourself with how to construct, render or interact with the react component in order to test your hook. You can just use the hook directly and assert the results. ## When to use this library 1. You're writing a library with one or more custom hooks that are not directly tied to a component 2. You have a complex hook that is difficult to test through component interactions ## When not to use this library 1. Your hook is defined alongside a component and is only used there 2. Your hook is easy to test by just testing the components using it ## Example ### `useCounter.js` ```js import { useState, useCallback } from 'react' function useCounter() { const [count, setCount] = useState(0) const increment = useCallback(() => setCount((x) => x + 1), []) return { count, increment } } export default useCounter ``` ### `useCounter.test.js` ```js import { renderHook, act } from '@testing-library/react-hooks' import useCounter from './useCounter' test('should increment counter', () => { const { result } = renderHook(() => useCounter()) act(() => { result.current.increment() }) expect(result.current.count).toBe(1) }) ``` More advanced usage can be found in the [documentation](https://react-hooks-testing-library.com/usage/basic-hooks). ## Installation ```sh npm install --save-dev @testing-library/react-hooks ``` ### Peer Dependencies `react-hooks-testing-library` does not come bundled with a version of [`react`](https://www.npmjs.com/package/react) to allow you to install the specific version you want to test against. It also does not come installed with a specific renderer, we currently support [`react-test-renderer`](https://www.npmjs.com/package/react-test-renderer) and [`react-dom`](https://www.npmjs.com/package/react-dom). You only need to install one of them, however, if you do have both installed, we will use `react-test-renderer` as the default. For more information see the [installation docs](https://react-hooks-testing-library.com/installation#renderer). Generally, the installed versions for `react` and the selected renderer should have matching versions: ```sh npm install react@^16.9.0 npm install --save-dev react-test-renderer@^16.9.0 ``` > **NOTE: The minimum supported version of `react`, `react-test-renderer` and `react-dom` is > `^16.9.0`.** ## API See the [API reference](https://react-hooks-testing-library.com/reference/api). ## Contributors Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

Michael Peyper

πŸ’» πŸ“– πŸ€” πŸš‡ 🚧 πŸ’¬ ⚠️

otofu-square

πŸ’»

Patrick P. Henley

πŸ€” πŸ‘€

Matheus Marques

πŸ’»

Dhruv Patel

πŸ› πŸ‘€

Nathaniel Tucker

πŸ› πŸ‘€

Sergei Grishchenko

πŸ’» πŸ“– πŸ€”

Josep M Sobrepere

πŸ“–

Marcel Tinner

πŸ“–

Daniel K.

πŸ› πŸ’»

Vince Malone

πŸ’»

Sebastian Weber

πŸ“

Christian Gill

πŸ“–

JavaScript Joe

βœ… ⚠️

Sarah Dayan

πŸ“¦

Roman Gusev

πŸ“–

Adam Seckel

πŸ’»

keiya sasaki

⚠️

Hu Chen

πŸ’» πŸ“– πŸ’‘

Josh

πŸ“– πŸ’¬ πŸ’» πŸ€” 🚧 ⚠️

Na'aman Hirschfeld

πŸ’»

Braydon Hall

πŸ’»

Jacob M-G Evans

πŸ’» ⚠️

Tiger Abrodi

πŸ’» ⚠️

Amr A.Mohammed

πŸ’» ⚠️

Juhana Jauhiainen

πŸ’»

Jens Meindertsma

πŸ’» ⚠️

Marco Moretti

πŸš‡

Martin V.

πŸ“–

Erozak

πŸ“–

Nick McCurdy

🚧

Arya

πŸ“–

numb86

πŸ“–

Alex Young

🚧

Ben Lambert

πŸ“–

David Cho-Lerat

πŸ“–

Evan Harmon

πŸ“–

Jason Brown

πŸ“–

KahWee Teng

πŸ“–

Leonid Shagabutdinov

πŸ“–

Levi Butcher

πŸ“–

Michele Settepani

πŸ“–

Sam

πŸ“–

Tanay Pratap

πŸ“–

Tom Rees-Herdman

πŸ“–

iqbal125

πŸ“–

cliffzhaobupt

🚧

Jon Koops

πŸ’»

Jonathan Peyper

πŸ‘€ πŸ’»

Sean Baines

πŸ“–

Mikhail Vasin

πŸ“–

Aleksandar Grbic

πŸ“–

Jonathan Holmes

πŸ’»

MichaΓ«l De Boey

🚧

Anton Zinovyev

πŸ› πŸ’»

marianna-exelate

πŸš‡

Matan Borenkraout

🚧

andyrooger

πŸ’»

Bryan Wain

πŸ› πŸ‘€

Robert Snow

⚠️

Chris Chen

⚠️

Masious

πŸ“–

Laishuxin

πŸ“–
This project follows the [all-contributors](https://allcontributors.org/) specification. Contributions of any kind welcome! ## Issues _Looking to contribute? Look for the [Good First Issue](https://github.com/testing-library/react-hooks-testing-library/issues?utf8=βœ“&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3A"good+first+issue"+) label._ ### πŸ› Bugs Please file an issue for bugs, missing documentation, or unexpected behavior. [**See Bugs**](https://github.com/testing-library/react-hooks-testing-library/issues?q=is%3Aissue+is%3Aopen+label%3Abug+sort%3Acreated-desc) ### πŸ’‘ Feature Requests Please file an issue to suggest new features. Vote on feature requests by adding a πŸ‘. This helps maintainers prioritize what to work on. [**See Feature Requests**](https://github.com/testing-library/react-hooks-testing-library/issues?q=is%3Aissue+sort%3Areactions-%2B1-desc+label%3Aenhancement+is%3Aopen) ### ❓ Questions For questions related to using the library, you can [raise issue here](https://github.com/testing-library/react-hooks-testing-library/issues/new), or visit a support community: - [Discord](https://discord.gg/testing-library) - [Stack Overflow](https://stackoverflow.com/questions/tagged/react-hooks-testing-library) ## LICENSE MIT