Handling Next.js Server-Side action Redirects in Cypress Tests
Published
2 July 2024
When working with Next.js server-side actions, it is often required to perform redirects for various reasons, such as user authentication, conditional rendering, or route changes based on server-side logic. These redirects can pose a challenge when running end-to-end tests using Cypress. By default, Cypress fails tests if it encounters uncaught exceptions, including those triggered by Next.js server-side redirects. In this blog post, we'll explore why and how to handle these exceptions to ensure your Cypress tests run smoothly.
The Challenge: NEXT_REDIRECT Exceptions
When performing a server-side redirect in Next.js, the redirect() function is often used. This function triggers a NEXT_REDIRECT error, which Next.js expects and handles internally to perform the actual redirect. However, these exceptions can cause Cypress tests to fail if not handled properly, as Cypress considers any uncaught exception a failure.
The Solution: Handling NEXT_REDIRECT Exceptions in Cypress
To ensure that your Cypress tests do not fail due to these expected redirects, you need to instruct Cypress to ignore NEXT_REDIRECT exceptions. This can be achieved by adding a custom handler for uncaught exceptions in your Cypress test, to add it to your support/commands.js file if you want to apply to all tests.
// Cypress configuration to handle uncaught exceptions related to NEXT_REDIRECTCypress.on("uncaught:exception", (err) => { console.log("err.message", err.message);
// Check if the error message includes "NEXT_REDIRECT" if (err.message.includes("NEXT_REDIRECT")) { // This block is added to handle server-side redirects in Next.js. // Next.js often performs server-side redirects for various reasons, such as: // - Authentication flows (redirecting to login page if not authenticated) // - Conditional rendering based on user data // - Route changes based on server-side logic // When these redirects occur, they can throw a "NEXT_REDIRECT" error, // which is expected behavior and should not cause the test to fail. // Returning false here prevents Cypress from failing the test when such an error is encountered. return false; }});Similar articles

Sorting Algorithms Explained
Bubble, selection, insertion, merge, quick, and heap sort: how each one works, where it shines, where it struggles, and animated demos that run the real algorithm code on the same starting data every time.
13 July 2026

Set Membership Testing: From Arrays to Bloom Filters
Every time Chrome warns you a site might be malicious, it just answered a membership question: is this URL in a set of bad ones? Here's how that check works, from a plain array to the probabilistic Bloom filter that makes it possible at scale.
10 July 2026

Why You Shouldn't Use Floating Point Math for Money in JavaScript
JavaScript numbers look like ordinary decimals, but they are stored as binary floating point. That is why 0.1 + 0.2 is not 0.3, and why checkout totals, tax lines, and ledger comparisons can quietly go wrong.
4 July 2026

RFC 10008: HTTP Finally Gets a Query Method
For thirty years, complex searches have been squeezed into GET query strings or smuggled through POST. RFC 10008 standardises QUERY: a safe, idempotent HTTP method with a request body. Here is why that matters and when you can actually use it.
3 July 2026
