TodayILearned

TIL… How to test a window-redirect using Mocks

My Blog has moved! You can find my latest content at daniel.scheufler.io! Please continue reading here.

I was testing an action, which upon completion needed to redirect the user to a new page. Normally, in javascript you can redirect using :

window.location = "newPath";

But that’s hard to test for. Thankfully there is a better way:

window.location.assign("newPath");

You can then mock the assign function and test for that call! My thanks to the creators of jest for that insight!

Standard

Leave a comment