A library for mocking WebSocket servers in Jest tests. Version 2.5.0 is the current stable release with no fixed cadence. It provides a WS class to simulate a WebSocket server, track messages, and assert interactions. Supports JSON protocol auto-serialization, error simulation, and graceful close. Ships TypeScript definitions. Key differentiators: built-in connected/closed/nextMessage promises for async test flow, static clean() method to reset between tests, and compatibility with popular patterns like redux-saga and react hooks via example code.
npm install jest-websocket-mockNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Creates a mock WebSocket server, connects a client, sends a message, and asserts the server received it.
Add WS.clean() in afterEach or afterAll to close all connections.
Change import { WS } from 'jest-websocket-mock' to import WS from 'jest-websocket-mock'.Track connections manually if you need per-connection state.
Use distinct URLs or clean up with WS.clean() before creating new instances.
Run npm install --save-dev jest-websocket-mock mock-socket
Change to import WS from 'jest-websocket-mock'
Add afterEach(() => { WS.clean(); });