Simple component wrapper and utilities for testing React hooks.
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 react-hooks-testing-library
is build on top of the wonderful react-testing-library
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.
Using this library, you do not have to concern yourself with how the to interact construct, render or interact with the react component in order to test your hook. You can just use the hook directly and assert the resulting values.
- You're writing a library with one or more custom hooks that are not directly tied a component
- You have a complex hook that is difficult to test through component interactions
- Your hook is defined along side a component and is only used there
- Your hook is easy to test by just testing the components using it
// useTheme.js
import { useState, createContext, useContext, useMemo } from 'react'
const themes = {
light: { primaryLight: '#FFFFFF', primaryDark: '#000000' },
dark: { primaryLight: '#000000', primaryDark: '#FFFFFF' }
}
const ThemesContext = createContext(themes)
const useTheme = (initialTheme) => {
const themes = useContext(ThemesContext)
const [theme, setTheme] = useState(initialTheme)
const toggleTheme = () => {
setTheme(theme === 'light' ? 'dark' : 'light')
}
return useMemo(() => ({ ...themes[theme], toggleTheme }), [theme])
}
// useTheme.test.js
import { renderHook, cleanup, act } from 'react-hooks-testing-library'
describe('custom hook tests', () => {
afterEach(cleanup)
test('should use theme', () => {
const { result } = renderHook(() => useTheme('light'))
const theme = result.current
expect(theme.primaryLight).toBe('#FFFFFF')
expect(theme.primaryDark).toBe('#000000')
})
test('should update theme', () => {
const { result } = renderHook(() => useTheme('light'))
const { toggleTheme } = result.current
act(() => toggleTheme())
const theme = result.current
expect(theme.primaryLight).toBe('#000000')
expect(theme.primaryDark).toBe('#FFFFFF')
})
test('should use custom theme', () => {
const customThemes = {
light: { primaryLight: '#AABBCC', primaryDark: '#CCBBAA' },
dark: { primaryLight: '#CCBBAA', primaryDark: '#AABBCC' }
}
const wrapper = ({ children }) => (
<ThemesContext.Provider value={customThemes}>{children}</ThemesContext.Provider>
)
const { result } = renderHook(() => useTheme('light'), { wrapper })
const theme = result.current
expect(theme.primaryLight).toBe('#AABBCC')
expect(theme.primaryDark).toBe('#CCBBAA')
})
})
npm install react-hooks-testing-library
// TODO: write this - see Example above for now
Thanks goes to these wonderful people (emoji key):
Michael Peyper 💻 🎨 📖 🤔 🚇 📦 |
---|
This project follows the all-contributors specification. Contributions of any kind welcome!
Looking to contribute? Look for the Good First Issue label.
Please file an issue for bugs, missing documentation, or unexpected behavior.
Please file an issue to suggest new features. Vote on feature requests by adding a 👍. This helps maintainers prioritize what to work on.
For questions related to using the library, you can raise issue here, or visit a support community:
MIT