Skip to content

ecosystem page for react-hooks-testing-library #641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions docs/ecosystem-react-hooks-testing-library.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
id: ecosystem-react-hooks-testing-library
title: react-hooks-testing-library
---

The [`react-hooks-testing-library`](https://github.com/testing-library/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.

```
npm install --save-dev @testing-library/react-hooks
```
Imagine we have a simple hook that we want to test:

```js
import { useState, useCallback } from 'react'

export default function useCounter() {
const [count, setCount] = useState(0)
const increment = useCallback(() => setCount((x) => x + 1), [])
return { count, increment }
}
```

To test `useCounter` we need to render it using the `renderHook` function provided by
`react-hooks-testing-library`:

```js
import { renderHook } from '@testing-library/react-hooks'
import useCounter from './useCounter'

test('should use counter', () => {
const { result } = renderHook(() => useCounter())

expect(result.current.count).toBe(0)
expect(typeof result.current.increment).toBe('function')
})
```

As you can see, the result's current value matches what is returned by our hook.


[react-hooks-testing-library github](https://github.com/testing-library/react-hooks-testing-library)

[react-hooks-testing-library docs](https://react-hooks-testing-library.com/)
3 changes: 2 additions & 1 deletion website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@
"ecosystem-riot-testing-library",
"ecosystem-jasmine-dom",
"ecosystem-query-extensions",
"ecosystem-rtl-simple-queries"
"ecosystem-rtl-simple-queries",
"ecosystem-react-hooks-testing-library"
],
"Help": ["faq", "learning", "contributing"]
},
Expand Down