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

AI-First Programming Languages: What They Are, and Whether You Should Use One Yet
In the last year a genuinely new category of programming language appeared: ones where the AI, not you, is the intended author. Here's what the categories are, which projects actually have traction, and which of them I'd be willing to put in production today.
9 Aug 2026

lucide-animated: Icon Motion That Knows When to Stop
lucide-animated wraps the entire lucide-react set in small, Motion-powered animations, same names, same currentColor stroke, one extra letter in the import. I wired it into a few real spots on this site and worked out where it earns its place and where it's just noise.
2 Aug 2026

Animated Gradient Borders: A Better Loading State for AI Interfaces
Spinners and skeleton screens assume a request either hasn't returned yet or has. AI responses don't work that way, they stream in over seconds while you're already reading them. A rotating gradient border solves the "still working" signal without covering up content the user is actively looking at.
31 July 2026

Pathfinding Algorithms Explained
A live stealth pathfinding demo, and the three buckets every algorithm in the dropdown falls into: Naive, Uninformed, and Informed. Deep dives live under /algorithms.
26 July 2026
