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

Published: 02 November 2021
on channel: Bruno Antunes
17,140
465

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


Watch video Unit Testing Next.js Router - useRouter - React.js Testing Tutorial #9 online without registration, duration hours minute second in high quality. This video was added by user Bruno Antunes 02 November 2021, don't forget to share it with your friends and acquaintances, it has been viewed on our site 17,140 once and liked it 465 people.