Testing Next.js Pages through Real Production Requests

A Passing Component Can Sit Behind the Wrong Response
A page that says "Repair not found" can still return HTTP 200. A component test can confirm every word in that heading and leave the status completely unchecked. That matters when someone arrives from a bookmark or when a crawler needs to know whether the URL still exists.
Then there's the person who clicks through from the listing. Next.js can request the page data and update the existing document. If the route redirects or the data request fails, I want to see where the browser lands and how it got there.
The repair café example uses Next.js 12.0.4, React 17.0.2, and Node.js 16.13.1. I run the requests against its production server on my machine.
Give the Routes Something to Disagree About
The catalogue and a guide to loose handles use getStaticProps. The guide says to keep the loose screws in a container. At least one part of this experiment works with the laptop off.
A desk lamp page uses getServerSideProps and asks a small local HTTP server for the repair record. The stub can send a complete record, a 404, an HTTP 200 with the instructions missing, or an HTTP 500. The loader treats each response according to what it can establish. A missing record means the route can say it's missing. A broken record tells us the route couldn't be served correctly. I want those two cases to look different in the request log as well as on the page.
The fixture also includes an old lamp URL and a booking page. The first redirects to the current repair. The second reads a synthetic session state per request and sends a signed‑out visitor to the sign‑in page. Fixed input lets the tests go through the actual router without relying on an external CMS or a login provider.
Start at the Address Bar
I run next build once, then run the checks against next start. The fixture's readLocalResponse helper uses Node's HTTP client, so it stops at the first response. A client that follows redirects can hand back a perfectly good destination and hide the response you wanted to test.
const response = await readLocalResponse(`${origin}/repairs/old-lamp/`);
assert.equal(response.status, 307);
assert.equal(response.headers.location, '/repairs/lamp/');There's a second redirect worth checking separately. With trailingSlash: true, requesting /repairs/lamp returns 308 to /repairs/lamp/. That came from the framework's URL policy. The 307 above came from the repair route's temporary redirect. Both end at the same desk lamp, which makes checking only the destination a rather forgiving test. I've written separately about choosing a redirect status.
For a successful repair or booking request I also read the __NEXT_DATA__ script from the HTML and check the exact pageProps. The repair response contains the title and instructions. The booking contains Ellie's name and time, and the response carries Cache-Control: private, no-store. The original records each contain a synthetic private note as a marker. The loader selects the fields it needs before returning props. The test checks for those markers in the initial document and the data sent during a Link transition. Props can be visible to the browser even if the component never renders them as text.
Follow the Link and Check Where the Browser Lands
The browser starts on the catalogue in a fresh context for each case. I focus the actual link and activate it with Enter. That avoids using pointer hover as an extra prefetch trigger. The test listens for the request at the build‑specific /_next/data/… URL and makes a separate assertion about the final URL and heading. It also marks the original document so I can tell whether it survived the navigation.
| Fixture case | Direct document | Link transition in the tested build |
|---|---|---|
| Desk lamp | 200 with the repair HTML and public props | 200 data response. The same document shows the lamp heading. |
| Missing record | 404 with the missing repair page | 404 data response carrying notFound: true. The same document shows the missing repair page. |
| Malformed record or upstream failure | 500 with the custom error page | 500 from the data request. Next.js makes a document request and displays the custom 500 page. |
| Old lamp URL | 307 redirect to the lamp | 200 data response with a redirect marker. The same document shows the lamp. |
| Signed‑out booking | 307 redirect to sign‑in | 200 data response with a redirect marker. The same document shows the sign‑in page. |
The missing‑record body parses as JSON even though that response has no Content-Type header. Both 500 responses carry text/html at a URL ending in .json. I use Playwright to capture the original status and the browser's document request, then make a separate HTTP request to check the 500 body. That keeps the body check independent of the navigation.
I check __N_REDIRECT in this fixture because I want to identify exactly what prompted the router to move. I'd keep that assertion in a versioned framework test and use the destination and visible outcome in the ordinary journey tests. My Next.js Link guide explains the navigation itself.
Keep the Release Check Small Enough to Run
My downloadable regression lab includes the catalogue, the stub, the expected route table, and the assertion scripts. It checks ten direct requests, eight Link transitions, and the stub's request log. Two further checks deliberately alter local observations. One substitutes a 200 for the missing response whilst keeping its heading. The other adds a private marker to a copy of the response data. Both fail the relevant assertion.
I'd keep a matrix like this for a few representative routes. A prebuilt guide tests a different path from a route that reads the CMS at request time. A protected page earns its own checks because the wrong outcome can expose data as well as send someone to the wrong screen. I'd check the headers again after deployment, where a host or shared cache can change what a visitor receives, as I explain in my Cache‑Control guide.
Postscript
November 2026: Next.js now has the App Router alongside the Pages Router used here. If you adapt this test matrix, check the streaming behaviour: once streaming has started, notFound() can show the missing‑page UI whilst the response remains HTTP 200. I'd keep separate assertions for the status and the page someone actually sees. The 404 expectations above describe this Pages Router fixture; they aren't a rule for every Next.js response.