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

Production Agentic AI: Permissions, Observability, and Kill Switches
Putting an agentic AI system into production is a different engineering problem to putting it into staging. Once it is live, the governance question shifts from "will it behave?" to "how do we know if it stops behaving, and what do we do about it?" A practical guide to production permissions, observability architecture, and kill switches for Australian engineering leaders in regulated industries.
25 Sept 2026

What Australian CTOs Should Put in the AI Governance Pack
The board is asking. The regulator is watching. The CTO is the one who has to translate "AI governance" from a policy concept into a document set that actually governs something. Here is what a credible AI governance pack looks like for an Australian organisation in 2026, and what each section needs to do to survive scrutiny.
21 Sept 2026

Governing Agentic AI: Human-in-the-Loop Patterns When Tools Can Act (MCP Edition)
Agentic AI doesn't just recommend. It acts. That changes governance from a policy question into an engineering question: which patterns actually limit blast radius, satisfy a human reviewer, and survive scrutiny from a regulator or court? A practical playbook for MCP-connected agents, with a checklist for regulated industries.
16 Sept 2026

Vercel Can Do WebSockets Now. Here's Where a Chat Room Still Breaks It.
Vercel shipped native WebSocket support in 2026, so a Function really can hold a socket open. But the connection is still a function invocation with a duration ceiling and no fan-out. Here's where that's fine for a chat app, where it isn't, and how Supabase Realtime covers the gap.
15 Aug 2026
