score:2
A good approach would be testing the component itself, that already contains the hook.
In case you consider that the hook needs to be test without the component, you can use @testing-library/react-hooks
package, something like:
const useFirstHook = (shouldThrow = false) => {
// throw onmount
useEffect(() => {
if (shouldThrow) throw new Error('my custom error is thrown');
}, [shouldThrow]);
return {
test1: 'I am test 1'
};
};
describe('useFirstHook', () => {
it('should not throw', () => {
const { result } = renderHook(() => useFirstHook(false));
expect(result.current.test1).toEqual('I am test 1');
});
it('should throw', () => {
try {
const { result } = renderHook(() => useFirstHook(true));
expect(result.current).toBe(undefined);
} catch (err) {
expect(err).toEqual(Error('my custom error is thrown'));
}
});
});
Source: stackoverflow.com
Related Query
- unit test custom hook with jest and react testing library
- Create unit test with Jest and React testing library for helper which generate a domain url
- How to test a className with the Jest and React testing library
- how can I test if useState hook has been called with jest and react testing library?
- How to test redux state update with react testing library and jest
- Having an issue while up and running unit test with react redux testing library
- Access and modify react context from jest test with react testing library
- How to Test Custom Hook with react testing library
- Unit testing react redux thunk dispatches with jest and react testing library for "v: 16.13.1",
- how to unit test a form that has state and method from a custom hook with jest
- How to mock third-party library custom events with jest and react testing library
- How to write a test with Jest and React testing library to check if anchor-tag <a /> exist
- how to test component while passing boolean value with react testing library and jest
- Unit test method that calls clearInterval with Jest and Enzyme on React
- How to properly test React Native Modals using Jest and Native Testing Library
- Unit test form submission with data using react testing library
- How to check the value of a nested React component in a unit test with Enzyme and Jest
- how to test Routes in react using jest and react testing library
- testing custom react methods with jest and enzyme
- stub fetch unit test react with enzyme and jest
- Catch problem with await in React 17, jest and testing library
- how to test button that call submit form using jest and react testing library
- An update to Provider inside a test was not wrapped in act(...) with Jest + React Testing Library
- Testing custom hook with SetTimeout and useEffect with Jest
- Unit test for useReducer hook with jest and enzyme
- How to Test React's Error Boundary using Jest and React Testing Library
- How do I unit test amcharts using jest with react and typescript?
- How to test a required input field with React Testing Library and Jest?
- Unit Testing with Jest and React - Axios Get Inside useEffect
- Unit Testing Custom React Hooks in TypeScript using Jest and @testing-library/react-hooks
More Query from same tag
- react-dom.development.js:287 Uncaught TypeError: Cannot read property 'value' of undefined
- EmailJS for multiple step React Form
- mapStateToProps() in Connect() must return a plain object. Instead received undefined
- is there solve for this Error: Could not find "store" in the context of "Connect(App)". Either wrap the root component in a <Provider>
- Refused to display in a frame because it set 'X-Frame-Options' to 'deny' in django application
- Spring Boot (inject script in reactjs)
- Cookie not sent when with request
- Keep getting TypeError: Cannot read property 'map' of undefined when I click on checkbox, dont know where the problem is
- Generating a PDF file from React Components
- Cannot get basic React with JSX example to work
- onChange handler isn't updating state when I type in input
- How to correctly use React `useCallback`'s dependencies list?
- React rails rendering home page view
- How do I change the number of cards displayed in row
- iframe onload fires when iframe is not ready
- How to fix Uncaught TypeError: Cannot read property 'getState' of undefined?
- Trouble setting conditional onClick behavior
- Cannot view picture React.js
- React useContext keep data after redirect?
- Can a default value be used in a proptype shape?
- How to get URL params when using Custom Document in NextJS
- Error Could not resolve dependency: redux-react-session
- Can a form have more than one on submit?
- How to render a page full of special charachters without escaping every single one
- how set Scroll to the top of the page after page change?
- Add an external child in React
- How to toggle class on onclick in map function?
- Is there any stripe element to display a saved card to user.?
- React Promise will not return a value
- useState doesn't update the value of my variable