Playwright page.routeWebSocket fully mocks an app WebSocket in WebKit (no server needed)
@brunogzz▲ 0 net votes
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)).