All Projects โ†’ testing-library โ†’ React Hooks Testing Library

testing-library / React Hooks Testing Library

Licence: mit
๐Ÿ Simple and complete React hooks testing utilities that encourage good testing practices.

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to React Hooks Testing Library

use-custom-compare-effect
useEffect hook which takes a comparison function instead of compare using reference equality
Stars: โœญ 26 (-99.37%)
Mutual labels:  react-hooks
react-hook-clipboard
A React hook for accessing clipboard
Stars: โœญ 16 (-99.61%)
Mutual labels:  react-hooks
redux-spring
ๅŸบไบŽ redux ๆจกๅ—ๅŒ–ๆก†ๆžถ
Stars: โœญ 20 (-99.52%)
Mutual labels:  react-hooks
covid
COVID-19 updates webpage
Stars: โœญ 117 (-97.18%)
Mutual labels:  react-hooks
swr-examples
SWR is a React Hook library for remote data fetching. Here are some simple examples.
Stars: โœญ 14 (-99.66%)
Mutual labels:  react-hooks
date-range-picker
โš›๏ธ๐Ÿ“† Flexible React date range picker calendar with no dependencies.
Stars: โœญ 94 (-97.73%)
Mutual labels:  react-hooks
MyApp
A template to create awesome Apps easily โšก๏ธ
Stars: โœญ 39 (-99.06%)
Mutual labels:  react-hooks
Wouter
๐Ÿฅข A minimalist-friendly ~1.5KB routing for React and Preact. Nothing else but HOOKS.
Stars: โœญ 3,654 (-11.87%)
Mutual labels:  react-hooks
React-Combine-Provider
combine react providers in ease
Stars: โœญ 29 (-99.3%)
Mutual labels:  react-hooks
react-hooks-uikit-express-oauth-boiler-v2
A React/TypeScript fullstack boilerplate utilizing React hooks, using Express as backend, UIKit for frontend, MongoDB for storage & open-authenticator for OAuth.
Stars: โœญ 110 (-97.35%)
Mutual labels:  react-hooks
React-Chat-App
This is a real time chat application with features of online status,read receipnt,image support and create multiple rooms
Stars: โœญ 13 (-99.69%)
Mutual labels:  react-hooks
shop-app
A shopping mobile application made with react native for Android.
Stars: โœญ 100 (-97.59%)
Mutual labels:  react-hooks
art-ui
๐ŸŒˆ A UI Design Language and React UI library
Stars: โœญ 34 (-99.18%)
Mutual labels:  react-hooks
react-contentful
๐Ÿ“ฐ A React component library that makes it super simple to compose Contentful content into your sites and applications.
Stars: โœญ 58 (-98.6%)
Mutual labels:  react-hooks
use-dencrypt-effect
โš› A custom React hook generating crypting text effect.
Stars: โœญ 39 (-99.06%)
Mutual labels:  react-hooks
Compose
Nice and simple DSL for Espresso Compose UI testing in Kotlin
Stars: โœญ 56 (-98.65%)
Mutual labels:  testing-library
use-smartcrop
React hook for smartcrop.js to content aware image cropping with points of interest and facial recognition.
Stars: โœญ 93 (-97.76%)
Mutual labels:  react-hooks
Constate
React Context + State
Stars: โœญ 3,519 (-15.12%)
Mutual labels:  react-hooks
weixin-cash
๐Ÿ’ฐ ไปฟๅพฎไฟก่ฎฐ่ดฆๅฐ็จ‹ๅบ็š„ React Appใ€‚
Stars: โœญ 20 (-99.52%)
Mutual labels:  react-hooks
use-simple-store
๐Ÿฌ Simple state management using React Hook
Stars: โœญ 21 (-99.49%)
Mutual labels:  react-hooks

react-hooks-testing-library

ram

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


Read The Docs

Build Status codecov version downloads MIT License

All Contributors PRs Welcome Code of Conduct Netlify Status Discord

Watch on GitHub Star on GitHub Tweet

Table of Contents

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

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

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.

Installation

npm install --save-dev @testing-library/react-hooks

Peer Dependencies

react-hooks-testing-library does not come bundled with a version of 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 and 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. Generally, the installed versions for react and the selected renderer should have matching versions:

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.

Contributors

Thanks goes to these wonderful people (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 specification. Contributions of any kind welcome!

Issues

Looking to contribute? Look for the Good First Issue label.

๐Ÿ› Bugs

Please file an issue for bugs, missing documentation, or unexpected behavior.

See Bugs

๐Ÿ’ก 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

โ“ Questions

For questions related to using the library, you can raise issue here, or visit a support community:

LICENSE

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].