@testing-library/react-hooks
    Overview
    Documentation
    Tutorials
    Chat
    Insights
    Code
    Dependencies
    Contributors
    Jobs

@testing-library/react-hooks

Claim

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

2.0.1  •  Updated 9 months ago  •  by testing-library  •  MIT License

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

Watch on GitHub Star on GitHub Tweet

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 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 along side 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 or react-test-renderer to allow you to install the specific version you want to test against. Generally, the installed versions for react and react-test-renderer should have matching versions:

npm install react@^16.9.0
npm install --save-dev react-test-renderer@^16.9.0

Both of these dependecies must be installed as at least version 16.9.0 to be compatible with react-hooks-testing-library.

API

See the API reference.

Contributors

Thanks goes to these wonderful people (emoji key):

Michael Peyper
Michael Peyper

💻 🎨 📖 🤔 🚇 📦 ⚠️ 🔧
otofu-square
otofu-square

💻
Patrick P. Henley
Patrick P. Henley

🤔 👀
Matheus Marques
Matheus Marques

💻
Dhruv Patel
Dhruv Patel

🐛 👀
Nathaniel Tucker
Nathaniel Tucker

🐛 👀
Sergei Grishchenko
Sergei Grishchenko

💻 📖 🤔
Josep M Sobrepere
Josep M Sobrepere

📖
Marcel Tinner
Marcel Tinner

📖
Daniel K.
Daniel K.

🐛 💻
Vince Malone
Vince Malone

💻

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

Popularity

Weekly Downloads
Add this graph to GitHub
185.2K

Maintenance

Development

Last ver 10 months ago
Created 1 year ago
Last commit 2 months ago
18 hours between commits

Technology

Node version: 12.6.0
22.4K unpacked

Compliance

MIT License
OSI Approved
0 vulnerabilities

Contributors

32 contributors
Michael Peyper
Maintainer, 404 commits, 252 merges, 6 PRs
Works at IOOF Holdings
Renovate Bot
217 commits
Works at whitesource
otofu-square
9 commits, 1 PRs
Works at TimeTree .inc
Sergey Grishchenko
6 commits, 1 PRs
Martin Veith
6 commits, 2 PRs
Works at Parkside IT GmbH
VinceMalone
4 commits

Tags

testing
react
hooks
unit
integration
Openbase helps developers choose among and use millions of open-source packages, so they can build amazing products faster.
FacebookTwitterLinkedIn
© 2020 Devstore, Inc.