@relaycorp/ws-mock
Mock client and server to unit test the NPM package ws
.
Install
npm install @relaycorp/ws-mock
Using the mock client
You should initialise MockClient
by passing the WebSocket server to be tested and then call client.connect()
to initiate the connection. From that point you can interact with the server. For example:
test('Challenge should be sent as soon as client connects', async () => {
const client = new MockClient(wsServer);
await client.connect();
const challengeSerialized = await client.receive();
expect(challengeSerialized).toBeInstanceOf(ArrayBuffer);
});
You'll find real-world examples in relaycorp/relaynet-internet-gateway.