title: How can I test a client-side redirect to a 3rd party site with Cypress?
body: <p>Cypress offers a simple way to test for server-side redirects using <a href="https://docs.cypress.io/api/commands/request.html#Syntax" rel="noreferrer">request</a>:</p>
  <pre><code>cy.request({
    url: `/dashboard/`,
    followRedirect: false, // turn off following redirects
  }).then((resp) =&gt; {
    expect(resp.redirectedToUrl).to.eq('http://example.com/session/new')
  })
  
  </code></pre>
  <p>However this doesn't work for client-side redirects because the page is loaded successfully before the redirect happens, meaning the response is for the page, not for the redirect.</p>
  <p><strong>How can I test a client-side redirect?</strong></p>
  <p>I need a way of catching the redirect and verifying that:</p>
  <ul>
  <li>it occurred</li>
  <li>was to the correct URL.</li>
  </ul>
  <p>Note:</p>
  <ul>
  <li>I don't want to follow the redirect away from the app that is being tested. I'm not testing the whole auth flow. I just need to know there was a redirect.</li>
  <li>I can't change this auth flow. The redirect is unavoidable.</li>
  <li>The redirect happens during initialisation, not as a result of any user interaction.</li>
  <li>The redirect uses: <code>window.location.href = url</code></li>
  <li>See my answer below for an attempt at resolving this.</li>
  </ul>