MCPcopy
hub / github.com/testing-library/react-hooks-testing-library

github.com/testing-library/react-hooks-testing-library @v8.0.1 sqlite

repository ↗ · DeepWiki ↗ · release v8.0.1 ↗
86 symbols 240 edges 52 files 0 documented · 0%
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

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 ⚠️

Core symbols most depended-on inside this repo

rerender
called by 22
src/dom/pure.ts
waitForNextUpdate
called by 12
src/core/asyncUtils.ts
waitFor
called by 10
src/core/asyncUtils.ts
addCleanup
called by 9
src/core/cleanup.ts
cleanup
called by 8
src/core/cleanup.ts
testHarness
called by 7
src/helpers/createTestHarness.tsx
hydrate
called by 7
src/server/pure.ts
getRenderer
called by 5
src/pure.ts

Shape

Function 83
Class 2
Method 1

Languages

TypeScript100%

Modules by API surface

src/core/index.ts9 symbols
src/core/asyncUtils.ts8 symbols
scripts/generate-submodules.ts6 symbols
src/server/pure.ts5 symbols
src/helpers/createTestHarness.tsx5 symbols
src/core/cleanup.ts5 symbols
src/native/pure.ts4 symbols
src/helpers/createTimeoutController.ts4 symbols
src/dom/pure.ts4 symbols
src/core/console.ts4 symbols
src/__tests__/errorHook.test.ts4 symbols
src/helpers/error.ts3 symbols

Used by 2 indexed graphs manifest dependencies, hub-wide

Dependencies from manifests, versioned

@babel/runtime7.12.5 · 1×
@types/react17.0.44 · 1×
@types/react-dom17.0.15 · 1×
@types/react-test-renderer17.0.1 · 1×
@typescript-eslint/eslint-plugin5.11.0 · 1×
@typescript-eslint/parser5.11.0 · 1×
all-contributors-cli6.20.0 · 1×
codecov3.8.3 · 1×
cross-env7.0.3 · 1×
docz2.3.1 · 1×
docz-theme-default1.2.0 · 1×
docz-utils2.3.0 · 1×

For agents

$ claude mcp add react-hooks-testing-library \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact