Skip to content

Response

BackendResponse represents a fulfilled, continued, passthrough, fetched, or waited-for HTTP response.

WARNING

Response accessors are methods. Use response.status() and await response.json(), not response.status or response.json.

Accessors

Method / propertyTypeDescription
response.ok()booleantrue for status 200 through 299.
response.url()stringFinal response URL.
response.status()numberHTTP status.
response.statusText()stringHTTP status text.
response.headers()Record<string, string>Response headers.
response.headersArray()Array<{ name: string; value: string }>Headers as name/value pairs.
response.headerValue(name)string | nullCase-insensitive header lookup.
response.body()Promise<Buffer>Body buffer.
response.text()Promise<string>UTF-8 body text.
response.json()Promise<unknown>JSON-parsed body.
response.request()BackendRequestRequest associated with the response.
response.dispose()Promise<void>Marks the response body as disposed.
response.bodyBufferBufferBuffered body for fulfill({ response }) helpers.

From route.fetch()

ts
await backendMocks.route("https://api.example.test/users", async (route) => {
  const response = await route.fetch();

  expect(response.ok()).toBe(true);
  expect(response.status()).toBe(200);

  const users = (await response.json()) as Array<{ id: number; name: string }>;
  await route.fulfill({
    response,
    json: users.filter((user) => user.id !== 0),
  });
});

From waitForResponse()

ts
const pending = backendMocks.waitForResponse(
  (response) => response.url().endsWith("/charges") && response.status() === 201,
);

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

const response = await pending;
expect(await response.json()).toEqual({ id: "ch_123" });

Dispose

dispose() mirrors Playwright's API response shape. After disposal, body(), text(), and json() throw.

ts
const response = await route.fetch();
const body = await response.json();
await response.dispose();

Do not dispose a response before passing it to route.fulfill({ response }); fulfilling with a disposed response throws.

Types

ts
interface BackendResponse {
  ok(): boolean;
  url(): string;
  status(): number;
  statusText(): string;
  headers(): Record<string, string>;
  headersArray(): Array<{ name: string; value: string }>;
  headerValue(name: string): string | null;
  body(): Promise<Buffer>;
  text(): Promise<string>;
  json(): Promise<unknown>;
  request(): BackendRequest;
  dispose(): Promise<void>;
  readonly bodyBuffer: Buffer;
}

Mock the outside world. Keep the real app.