Giter Site home page Giter Site logo

tigerabrodi / react-hooks-testing-library Goto Github PK

View Code? Open in Web Editor NEW

This project forked from testing-library/react-hooks-testing-library

0.0 1.0 0.0 10.11 MB

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

Home Page: https://react-hooks-testing-library.com

License: MIT License

JavaScript 100.00%

react-hooks-testing-library's Introduction

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

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

NOTE: The minimum supported version of react and react-test-renderer 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

๐Ÿ“–

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

react-hooks-testing-library's People

Contributors

102 avatar 7michele7 avatar ab18556 avatar adamseckel avatar aryyya avatar cliffzhaobupt avatar dependabot[bot] avatar doppelmutzi-netpioneer avatar erozak avatar gillchristian avatar joshuaellis avatar jsjoeio avatar kahwee avatar keiya01 avatar levibutcher avatar marquesm91 avatar mpeyper avatar mtinner avatar ndresx avatar nickmccurdy avatar ntucker avatar numb86 avatar otofu-square avatar renovate-bot avatar samnoh avatar sarahdayan avatar sgrishchenko avatar shagabutdinov avatar tanaypratap avatar techanvil avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.