Skip to content

Abort and failures

Use route.abort() to make an outbound Node request fail like a network error.

ts
await backendMocks.route("https://api.example.test/users", async (route) => {
  await route.abort("timedout");
});

abort() is terminal. The Node agent rejects the intercepted request, and request.response() resolves to null.

Abort codes

The public route type accepts Playwright abort strings, but the current wire protocol carries this supported subset:

CodeDefault message
failednet::ERR_FAILED
abortednet::ERR_ABORTED
timedoutnet::ERR_TIMED_OUT
connectionrefusednet::ERR_CONNECTION_REFUSED
connectionresetnet::ERR_CONNECTION_RESET
namenotresolvednet::ERR_NAME_NOT_RESOLVED

Omitting the code is equivalent to abort("failed").

TIP

Unsupported Playwright abort strings are accepted by the TypeScript route API but collapse to failed on the wire.

Test user-visible failure handling

ts
test("shows retry messaging when payments time out", async ({ page, backendMocks }) => {
  await backendMocks.route("https://payments.example.test/**", async (route) => {
    await route.abort("timedout");
  });

  await page.goto("/checkout");
  await page.getByRole("button", { name: "Pay" }).click();

  await expect(page.getByText(/timed out|try again/i)).toBeVisible();
});

Different HTTP clients wrap network failures differently. Assert on your application behavior, not the internal error class, unless you are testing the agent itself.

Handler failures

If a route handler throws, the fixture records the error and aborts the paused Node request with failed so your app does not hang.

ts
await backendMocks.route("https://api.example.test/**", async () => {
  throw new Error("test setup failed");
});

Undrained errors fail fixture teardown as an AggregateError.

ts
const errors = backendMocks.takeErrors();
expect(errors[0]?.message).toContain("test setup failed");

Use takeErrors() only when the test intentionally triggers the error path.

Claim failures

FailureWhat happened
ambiguous_routeMore than one test claimed the same request.
claim_timeoutA test with active routes did not answer a claim before the proxy timeout.
disconnectedThe owning Playwright worker or Node agent disconnected while a request was active.
internalThe proxy could not complete coordination.

See Errors for the operational reference and Troubleshooting for fixes.

Mock the outside world. Keep the real app.