ランキングに戻る

testing-library/react-hooks-testing-library

TypeScriptreact-hooks-testing-library.com

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

reactjstestingreact-hooksjavascripttesting-library
スター成長
スター
5.3k
フォーク
229
週間成長
Issue
29
2k4k
2018年11月2021年5月2023年12月2026年7月
成果物npmnpm install react-hooks-testing-library
README

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

関連リポジトリ
pmndrs/zustand

🐻 Bear necessities for state management in React

TypeScriptnpmMIT Licensehooksreact
zustand-demo.pmnd.rs
58.5k2.2k
twentyhq/twenty

The open alternative to Salesforce, designed for AI.

TypeScriptnpmOthercrmcustomer
twenty.com
53.4k8.1k
react-hook-form/react-hook-form

📋 React Hooks for form state management and validation (Web + React Native)

TypeScriptnpmMIT Licensetypescriptreact-hooks
react-hook-form.com
44.8k2.5k
sudheerj/reactjs-interview-questions

List of top 500 ReactJS Interview Questions & Answers....Coding exercise questions are coming soon!!

JavaScriptnpmMIT Licensereactjsreact-router
44.7k10.4k
DavidHDev/react-bits

An open source collection of animated, interactive & fully customizable React components for building memorable websites.

JavaScriptnpmOtheranimationscomponents
reactbits.dev
43.9k2.1k
chakra-ui/chakra-ui

Chakra UI is a component system for building SaaS products with speed ⚡️

TypeScriptnpmMIT Licensechakra-uireact
chakra-ui.com
40.5k3.6k
novuhq/novu

The open-source communication infrastructure for agents and products

TypeScriptnpmOthernotificationscommunication
go.novu.co/github
39.3k4.4k
ToolJet/ToolJet

ToolJet is the open-source foundation of ToolJet AI - the enterprise app generation platform for building internal tools, dashboard, business applications, workflows and AI agents 🚀

JavaScriptnpmGNU Affero General Public License v3.0javascriptinternal-tools
tooljet.com
38.2k5.2k
NervJS/taro

开放式跨端跨框架解决方案,支持使用 React/Vue 等框架来开发微信/京东/百度/支付宝/字节跳动/ QQ 小程序/H5/React Native 等应用。 https://taro.zone/

TypeScriptnpmOthertaroreact
docs.taro.zone
37.6k4.9k
CopilotKit/CopilotKit

The Frontend Stack for Agents & Generative UI. React, Angular, Mobile, Slack, and more. Makers of the AG-UI Protocol

TypeScriptnpmMIT Licenseaillm
docs.copilotkit.ai
36.2k4.5k
conductor-oss/conductor

Conductor is an event driven agentic workflow engine providing durable and highly resilient execution engine for applications and AI Agents

JavaMavenApache License 2.0distributed-systemsgrpc
docs.conductor-oss.org
32k963
SigNoz/signoz

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

TypeScriptnpmOtherobservabilityapplication-monitoring
signoz.io
31.5k2.4k