A mock WebSocket server for Vitest tests, forked from jest-websocket-mock. Current stable version is 0.5.0. It provides a WS class to create mock servers, track messages, and send responses, with Vitest-specific matchers and helpers. Works with Vitest >=3 and TypeScript types are included. Key differentiators: seamless Vitest integration (no Jest dependency), supports jsonProtocol, and includes async promises (connected, nextMessage, closed) for precise test timing.
npm install vitest-websocket-mockNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Demonstrates creating a mock WebSocket server, connecting a client, sending and receiving messages, and cleaning up after the test.
Await server.connected again for each new incoming connection, or use server.on('connection', handler).Use a unique port per test file or set a random port and pass it to both server and client.
Upgrade vitest to version 3 or higher. If using an older vitest, stick with vitest-websocket-mock <0.5.0.
Wrap message reception in loops or use server.on('message', handler) to collect all messages.When jsonProtocol is true, send and receive JavaScript objects directly; no manual conversion needed.
Use 'import WS from 'vitest-websocket-mock'' (default import).
Ensure the port is available, and consider increasing timeout with vi.setTimeout(10000) for slow environments.
Use 'ws://127.0.0.1:1234' instead of 'ws://localhost:1234' to force IPv4.
Install types: 'npm install -D @types/ws' or use a recent version of vitest-websocket-mock that ships types.