• Stars
    star
    5,083
  • Rank 8,188 (Top 0.2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

A Note about React 18 Support

If you are using the current version of react-testing-library, replace

import { renderHook } from '@testing-library/react-hooks'

with

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) and react-native-testing-library (PR) 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

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

More Repositories

1

react-testing-library

🐐 Simple and complete React DOM testing utilities that encourage good testing practices.
JavaScript
18,952
star
2

jest-dom

πŸ¦‰ Custom jest matchers to test the state of the DOM
JavaScript
4,422
star
3

dom-testing-library

πŸ™ Simple and complete DOM testing utilities that encourage good testing practices.
JavaScript
3,271
star
4

user-event

πŸ• Simulate user events
TypeScript
2,181
star
5

cypress-testing-library

πŸ… Simple and complete custom Cypress commands and utilities that encourage good testing practices.
JavaScript
1,798
star
6

vue-testing-library

🦎 Simple and complete Vue.js testing utilities that encourage good testing practices.
JavaScript
1,071
star
7

eslint-plugin-testing-library

ESLint plugin to follow best practices and anticipate common mistakes when writing tests with Testing Library
TypeScript
979
star
8

testing-playground

Simple and complete DOM testing playground that encourage good testing practices.
JavaScript
776
star
9

angular-testing-library

πŸ¦” Simple and complete Angular testing utilities that encourage good testing practices
TypeScript
626
star
10

svelte-testing-library

🐿️ Simple and complete Svelte DOM testing utilities that encourage good testing practices
JavaScript
615
star
11

native-testing-library

🐳 Simple and complete React Native testing utilities that encourage good testing practices.
JavaScript
515
star
12

testing-library-docs

docs site for @testing-library/*
JavaScript
449
star
13

jest-native

πŸ¦… Custom jest matchers to test the state of React Native
TypeScript
436
star
14

eslint-plugin-jest-dom

eslint rules for use with jest-dom
JavaScript
360
star
15

pptr-testing-library

puppeteer + dom-testing-library = πŸ’–
TypeScript
283
star
16

playwright-testing-library

πŸ” Find elements in Playwright with queries from Testing Library
TypeScript
248
star
17

testing-library-recorder-extension

Testing Library Extension for Chrome DevTools Recorder
TypeScript
144
star
18

preact-testing-library

Simple and complete Preact DOM testing utilities that encourage good testing practices.
JavaScript
139
star
19

which-query

🦩 Which query should I use?
CSS
124
star
20

testcafe-testing-library

πŸ‚ Simple and complete custom Selectors for Testcafe that encourage good testing practices.
TypeScript
71
star
21

preact-hooks-testing-library

Simple and complete Preact hooks testing utilities that encourage good testing practices.
TypeScript
56
star
22

jasmine-dom

πŸ¦₯ Custom Jasmine matchers to test the state of the DOM
JavaScript
45
star
23

nightwatch-testing-library

πŸ¦‡Simple and complete custom queries for Nightwatch that encourage good testing practices.
JavaScript
31
star
24

dom-testing-library-template

Template repository for bug reports to @testing-library/dom, @testing-library/react, and @testing-library/jest-dom
JavaScript
17
star
25

webdriverio-testing-library

πŸ•·οΈ Simple and complete WebdriverIO DOM testing utilities that encourage good testing practices.
TypeScript
16
star
26

native-testing-library-docs

🐳 Docs site for native-testing-library
JavaScript
16
star
27

react-testing-library-help

Fork this repo to reproduce your issue
HTML
12
star
28

web-testing-library

πŸ™ Experimental Web testing utilities that encourage good testing practices.
JavaScript
3
star