Testing custom Hooks
Now our blog application fully makes use of Hooks! We even defined custom Hooks for various functions to make our code more reusable, concise, and easy to read.
When creating custom Hooks, it also makes sense to write unit tests for them to ensure they work properly, even when we change them later on or add more options. We are going to use Vitest to write our unit tests. Vitest and Vite go very well together, because Vitest can read and use Vite configurations. Vitest also offers a Jest-compatible API. Jest is another very popular testing framework. If you are already familiar with Jest, learning Vitest will be a breeze. Additionally, Vitest is very fast and well equipped for modern web apps.
However, as a result of the rules of Hooks, we cannot call Hooks from the test functions because they can only be called inside the body of a functional React component. As we do not want to create a component specifically for each test, we are going to use the React...