Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/testing-library/react-hooks-testing-library
π Simple and complete React hooks testing utilities that encourage good testing practices.
https://github.com/testing-library/react-hooks-testing-library
javascript react-hooks reactjs testing testing-library
Last synced: 5 days ago
JSON representation
π Simple and complete React hooks testing utilities that encourage good testing practices.
- Host: GitHub
- URL: https://github.com/testing-library/react-hooks-testing-library
- Owner: testing-library
- License: mit
- Created: 2018-11-10T09:57:27.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2024-01-30T20:25:46.000Z (12 months ago)
- Last Synced: 2024-10-29T09:46:41.419Z (3 months ago)
- Topics: javascript, react-hooks, reactjs, testing, testing-library
- Language: TypeScript
- Homepage: https://react-hooks-testing-library.com
- Size: 10.6 MB
- Stars: 5,255
- Watchers: 31
- Forks: 233
- Open Issues: 47
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- awesome-react-cn - react-hooks-testing-library - React hooks testing utilities that encourage good testing practices (Uncategorized / Uncategorized)
- awesome - react-hooks-testing-library - π Simple and complete React hooks testing utilities that encourage good testing practices. (JavaScript)
- awesome-react - react-hooks-testing-library - Simple and complete React hooks testing utilities that encourage good testing practices. ![](https://img.shields.io/github/stars/testing-library/react-hooks-testing-library.svg?style=social&label=Star) (Dev Tools / Test)
- awesome - react-hooks-testing-library - Simple and complete React hooks testing utilities that encourage good testing practices. (Uncategorized / Uncategorized)
- awesome-list - react-hooks-testing-library - library | 3820 | (TypeScript)
- stars - react-hooks-testing-library - library | 5266 | (TypeScript)
- stars - react-hooks-testing-library - library | 5262 | (TypeScript)
README
react-hooks-testing-library
Simple and complete React hooks testing utilities that encourage good testing practices.
[![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