Dalius's blog

Friday, March 1, 2024

Frontend testing: hooks

Sometimes it happens that business logic is put inside a hook and it is logical to test it isolated. However you can’t call hook directly. One recommended way to test hook is to test component with that hook. That can be done, but hook might be complicated and testing it through interactions is too cumbersome. In that situation react-hooks-testing-library can be used (if you use reasonably new react testing-library version, then you already have it).

I personally found this library a huge time saver when I need to test hooks. It is documented well and it is possible to test even quite complicated cases. So far my experience with it that it does not necessary help to avoid bugs, but it helped to refactor code with confidence when time came.

This time the post is short one 😉