Unit Testing Next.js Router - useRouter - React.js Testing Tutorial #9

Published: 02 November 2021
on channel: Bruno Antunes
17,525
458

In this video we will unit-test components that use the Next.js useRouter hook.

We will also see how to have unit-tests side by side with the pages components, without opening new routes for those .spec.tsx files

GitHub URL: https://github.com/bmvantunes/youtube...

Join the channel to support my content:
   / @brunoantunespt  

Follow me on:
Twitter:   / bmvantunes  
Dev.to: https://dev.to/bmvantunes
Website: https://brunoantunes.net
GitHub: https://github.com/bmvantunes
LinkedIn:   / bmvantunes  

Timeline:
00:00 Introduction
00:06 Why useRouter() is null in unit tests?
02:10 Common online solution to mock useRouter
02:43 Create Mock Router function
04:35 How to unit test useRouter by providing RouterContext Provider
06:48 Link and useRouter unit-test
08:36 Navigate via code, by calling router.push unit test
11:32 Spec files side by side with page components

#jest #ReactTestingLibrary #nextjs


On this page of the site you can watch the video online Unit Testing Next.js Router - useRouter - React.js Testing Tutorial #9 with a duration of hours minute second in good quality, which was uploaded by the user Bruno Antunes 02 November 2021, share the link with friends and acquaintances, this video has already been watched 17,525 times on youtube and it was liked by 458 viewers. Enjoy your viewing!