I’m using Jasmine for some testing, although this can be generally applied to browser-based javascript unit testing.
I have a function that, on certain conditions redirects the user to a different page using window.location.assign. The problem is, if this line is reached, the page is redirected. In this case, since it’s redirected to '/', the page reloads, and all the tests run again. What can I do to test that the function reaches the line where it redirects, without redirecting?
I have faced this same problem. My solution was to break out the actual redirect into a single purpose function. That is, don’t do any condition checking or other logic, just redirect.
Say this is the old code…
I would change that to..
Then you can
spyOnthedoRedirectfunction to ensure the redirect function is passing in the correct URI for the conditions.