import {
connectWebSocket,
createMockClientWebSocketConstructor,
} from '@rest-vir/define-service';
const webSocket = await connectWebSocket(myService.webSockets['/my-path'], {
webSocketConstructor: createMockClientWebSocketConstructor(
myService.webSockets['/my-path'],
{preventImmediateOpen: true},
),
});
// mock server responses without an actual server
webSocket.sendFromHost(myMessage);
Use this to create a mock WebSocket constructor for unit testing Web Socket connection.
This creates a MockClientWebSocket constructor for connections from the client side with types for the given WebSocketDefinition and utilizing the given MockClientWebSocketOptions instance. This can be passed to
connectWebSocket
as thewebSocketConstructor
to allow unit testing on a client without spinning up an entire host to serve the WebSocket connection.