rest-vir - v1.0.0
    Preparing search index...

    Class MockClientWebSocket<WebSocketToConnect>

    A mock WebSocket constructor for connections from the client side. This can be passed to connectWebSocket as the webSocketConstructor to allow unit testing on a client without spinning up an entire host to serve the WebSocket connection.

    import {connectWebSocket, MockClientWebSocket} from '@rest-vir/define-service';

    const webSocket = await connectWebSocket(myService.webSockets['/my-path'], {
    webSocketConstructor: MockClientWebSocket<(typeof myService.webSockets)['/my-path']>,
    });

    // mock server responses without an actual server
    webSocket.sendFromHost(myMessage);

    Type Parameters

    Implements

    Index

    Constructors

    Properties

    Mocked WebSocket event listeners. While this is public, you should attach a listener using MockClientWebSocket.addEventListener method and remove them with MockClientWebSocket.removeEventListener, as you would with a normal WebSocket instance.

    readyState: CommonWebSocketState = CommonWebSocketState.Connecting

    Implements and mocks the standard WebSocket.readyState property.

    sendCallback:
        | undefined
        | MockClientWebSocketClientSendCallback<WebSocketToConnect>

    This callback will be called whenever a message is sent to or from the WebSocket. This is set via the constructor option sendCallback.

    import {MockClientWebSocket, connectWebSocket} from '@rest-vir/define-service';

    const webSocket = await connectWebSocket(myService.webSockets['/my-socket'], {
    webSocketConstructor: createMockClientWebSocketConstructor(
    myService.webSockets['/my-socket'],
    {
    sendCallback: (message) => {
    // handle the sent message here
    console.log('handled message:', message);
    },
    },
    ),
    });

    Methods