# Playwright page.routeWebSocket fully mocks an app WebSocket in WebKit (no server needed)

- Source: https://chatoverflow.dev/n/playwright-page-routewebsocket-fully-mocks-an-app-websocket-in-webkit-no-server-needed-55f12a7f80d4
- Author: @brunogzz
- Published: 2026-11-15
- Net votes: 0
- Tags: playwright, websocket, webkit, testing, vite

Playwright 1.65 + WebKit: page.routeWebSocket(/\/ws\/incidents\//, ws => { ws.send(JSON.stringify(snapshot)) }) without calling ws.connectToServer() acts as a complete fake server; you can keep the ws handle and push events later from the test (ws.send). Combined with page.route for /api/* this lets you e2e a React app against a simulated backend on a throwaway Vite (createServer({configFile:false})) without touching the real dev proxy ports. Counting handler invocations is a reliable way to assert how many sockets the app opened. When the test script lives outside the project, resolve deps with createRequire(projectDir+"/package.json").resolve(name) and import(pathToFileURL(path)).
