Skip to content

Configuration

Configuration lives in three places: the proxy process, the Playwright fixture, and the Node agent.

Environment variables

VariableUsed byDescription
PLAYWRIGHT_BACKEND_MOCKS_PROXY_URLNode agent, Playwright fixture defaultProxy base URL, for example http://127.0.0.1:4310.
PLAYWRIGHT_BACKEND_MOCKS_TOKENNode agent, Playwright fixture defaultOptional shared token for proxy WebSocket handshakes.

When the Node agent has no proxy URL, startBackendMocks() is a no-op.

Playwright fixture options

ts
import { defineConfig } from "@playwright/test";
import type { BackendMocksWorkerOptions } from "@playwright-backend-mocks/playwright";

export default defineConfig<object, BackendMocksWorkerOptions>({
  use: {
    backendMocksProxyUrl: "http://127.0.0.1:4310",
    backendMocksToken: process.env.PLAYWRIGHT_BACKEND_MOCKS_TOKEN,
  },
});
OptionTypeDefault
backendMocksProxyUrlstringprocess.env.PLAYWRIGHT_BACKEND_MOCKS_PROXY_URL ?? "http://127.0.0.1:4310"
backendMocksTokenstring | undefinedprocess.env.PLAYWRIGHT_BACKEND_MOCKS_TOKEN

Node agent options

ts
import { startBackendMocks } from "@playwright-backend-mocks/node";

const agent = await startBackendMocks({
  proxyUrl: process.env.PLAYWRIGHT_BACKEND_MOCKS_PROXY_URL,
  token: process.env.PLAYWRIGHT_BACKEND_MOCKS_TOKEN,
  clientId: "api-server",
});
OptionTypeDefaultDescription
proxyUrlstringPLAYWRIGHT_BACKEND_MOCKS_PROXY_URLProxy base URL. If missing or empty, the agent is a no-op.
clientIdstringnode-${process.pid}Stable process identity for matching and diagnostics.
tokenstringPLAYWRIGHT_BACKEND_MOCKS_TOKENShared token for the proxy handshake.

Proxy CLI options

bash
playwright-backend-mocks-proxy \
  --host 127.0.0.1 \
  --port 4310 \
  --claim-timeout-ms 5000
FlagDefaultDescription
--host <host>127.0.0.1Bind host.
--port <port>4310Bind port.
--token <token>noneOptional shared connection token.
--history-limit <n>1000In-memory history entry limit.
--heartbeat-ms <ms>15000WebSocket ping interval.
--idle-timeout-ms <ms>60000Disconnect idle sockets after this many milliseconds.
--claim-timeout-ms <ms>5000Wait time for Playwright tests to answer a route claim.
--log-level <level>infosilent, error, warn, info, or debug.
-h, --helpPrint help.

Programmatic proxy

ts
import { createProxyServer } from "@playwright-backend-mocks/proxy";

const server = createProxyServer({
  port: 4310,
  claimTimeoutMs: 5000,
  logLevel: "info",
});

await server.start();
console.log(server.url);

See Proxy operations.

Mock the outside world. Keep the real app.