My FeedDiscussionsHashnode Enterprise
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

How to mock React Hooks?

Deactivated User's photo
Deactivated User
·Feb 19, 2019

The latest version of React introduces Hooks. Some 3rd-party libraries already provide hooks as an alternative to HOCs, e.g. the translation library react-i18next.

Mocking some functionality is easy using higher order components, we just can export the main component (without the HOC) and pass a mocked function to the props. Is there a generic way (provided by React) how hooks can be mocked?

Some hooks like useState are an important part of the component and there's no need to mock them, but I would like to mock other hooks (e.g. translation or redux if they provide a hook in a future release).