Errors, timeouts and empty states
Why This Matters
Users meet error states more often than teams test them. A backend that is down for two minutes, a payment provider that times out, a mobile connection that drops: each one shows a screen that nobody looked at during development. Mocked failures make those unhappy paths part of the suite, so you know what users see when things go wrong.
A server error
test("a server error while loading products is shown to the user", async ({ page }) => {
await page.route(PRODUCTS, (route) =>
route.fulfill({ status: 500, json: { error: { code: "INTERNAL_ERROR", message: "Database unavailable" } } }),
);
await page.goto("/shopping");
await expect(page.getByTestId("products-error")).toHaveText("Could not load products: Database unavailable");
});
The stub uses the API’s real error shape, { error: { code, message } }, so the frontend reads it the way it would read a real failure. The shop puts the server’s message after a fixed prefix, in an element with role="alert", so screen readers announce it as well.
A network failure
test("a network failure is shown too", async ({ page }) => {
await page.route(PRODUCTS, (route) => route.abort("failed"));
await page.goto("/shopping");
// The browser's own wording follows the prefix, and differs per browser.
await expect(page.getByTestId("products-error")).toContainText("Could not load products:");
});
With abort there is no response at all, so the message comes from the browser. Chromium, Firefox and WebKit each word it differently, which is why this test checks only the shop’s own prefix. Assert on what your application controls.
A failing checkout
The most expensive error to get wrong is the one at checkout. This test lets the payment step fail:
test("a failing checkout shows the error, not a blank page", async ({ page }) => {
await page.route("**/api/shop/orders", (route) =>
route.request().method() === "POST"
? route.fulfill({
status: 503,
json: { error: { code: "SERVICE_UNAVAILABLE", message: "Payment service is down. Please try again later." } },
})
: route.fallback(),
);
// ... add the Desk Lamp, open the cart, choose PayPal, place the order ...
await expect(page.getByTestId("checkout-error")).toHaveText("Payment service is down. Please try again later.");
await expect(page.getByTestId("payment-success")).toHaveCount(0);
});
Two details:
route.request().method()limits the stub to placing an order. Anything else on that URL goes on withroute.fallback().- The second assertion checks what must not happen: no success screen. A test that only looks for the error would pass even if the page showed both.
Compare: the real backend
Not every error needs a mock. Place an order with an empty billing form and the real API answers 400 VALIDATION_ERROR with one entry per field. The shop shows each one under its field:
await expect(page.getByTestId("error-billing-email")).toHaveText("Email is required.");
await expect(page.getByTestId("error-billing-city")).toHaveText("City is required.");
This is the better test for validation, because it also checks that frontend and backend agree on the field names. Mock what you cannot produce on demand; use the real thing for what you can.
Slow responses, two ways
test("a delay in the route: the loading state is visible", async ({ page }) => {
await page.route(PRODUCTS, async (route) => {
await new Promise((resolve) => setTimeout(resolve, 2000));
await route.continue();
});
await page.goto("/shopping");
await expect(page.getByTestId("products-loading")).toBeVisible();
await expect(page.getByTestId("products-loading")).toBeHidden();
});
The practice site also offers a server-side delay: an X-Delay-Ms header makes the server itself wait. A route can add that header to the real request:
await page.route(PRODUCTS, (route) =>
route.continue({ headers: { ...route.request().headers(), "x-delay-ms": "2000" } }),
);
Delay in page.route | X-Delay-Ms on the server | |
|---|---|---|
| Where the wait happens | In the test, before the request leaves | On the server, after it arrives |
| Works on any site | Yes | Only where the server supports it |
| What it tests | How the frontend handles a late answer | The same, plus the real network path |
Real run
All eight mocked and unmocked tests passed in 7.4 seconds with seven workers:
✓ 1 …ork.spec.ts:48:3 › unhappy paths › a network failure is shown too (3.6s)
✓ 2 …appy paths › a failing checkout shows the error, not a blank page (4.0s)
✓ 4 …:3 › stubbing the backend › an empty result shows the empty state (3.7s)
✓ 5 …aths › a server error while loading products is shown to the user (3.6s)
✓ 6 … without mocking: the real API reports the missing billing fields (4.0s)
✓ 7 …25:3 › stubbing the backend › a real response, changed on the way (3.7s)
✓ 8 …ow responses › a delay in the route: the loading state is visible (4.1s)
✓ 9 …w responses › a delay on the server: the site's X-Delay-Ms header (3.1s)
Exercise
What happens when the products API fails after the page was fine? Write a test that loads the shop normally, then makes the products API fail, and searches for “lamp”. What does the user see?
Show suggested solution
test("a failure during search is shown", async ({ page }) => {
await page.goto("/shopping");
await expect(page.getByTestId(/^product-\d+$/).first()).toBeVisible();
await page.route(PRODUCTS, (route) =>
route.fulfill({ status: 500, json: { error: { code: "INTERNAL_ERROR", message: "Search is down" } } }),
);
await page.getByRole("textbox", { name: "Search for items" }).fill("lamp");
await expect(page.getByTestId("products-error")).toHaveText("Could not load products: Search is down");
});
Routes can be added at any moment, not only before goto. That makes it easy to test the transition from a working page to a broken one, which is closer to what users experience than a page that never worked.