5.2.11 • Published 10 days ago

@relaycorp/ws-mock v5.2.11

Weekly downloads
130
License
MIT
Repository
github
Last release
10 days ago

@relaycorp/ws-mock

Mock client and server to unit test servers and clients powered by ws.

Install

npm install @relaycorp/ws-mock

Using the mock client

You'd use a mock client when you need to test a server. You should initialise MockClient by passing the ws 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 and relaycorp/awala-gateway-desktop.

Using the mock server

You'd use a mock server when you need to test a client. You basically need to initialise MockServer and replace the default export from ws with a mock WebSocket. Here's an example with Jest:

let mockServer: MockServer;
beforeEach(() => {
  mockServer = new MockServer();
});
jest.mock('ws', () => ({
  __esModule: true,
  default: jest.fn().mockImplementation(() => mockServer.client),
}));

test('Server message should be played back', async () => {
  const clientUnderTest = new ClientUnderTest();
  const messageToEcho = 'foo';

  await mockServer.use(
    clientUnderTest.connectToServerAndInteractWithIt(),
    async () => {
      await mockServer.send(messageToEcho);

      // Check that the client sent the message back to the server:
      const clientResponse = await mockServer.receive();
      expect(clientResponse).toEqual(messageToEcho);
    },
  );
});

You'll find real-world examples in relaycorp/relaynet-poweb-js.

Using streams

When using streams in the unit under test, make sure to mock the createWebSocketStream function in ws. Here's an example with Jest:

import { createMockWebSocketStream } from '@relaycorp/ws-mock';
import WebSocket from 'ws';

jest
  .spyOn(WebSocket, 'createWebSocketStream')
  .mockImplementation(createMockWebSocketStream);
5.2.11

10 days ago

5.2.10

4 months ago

5.2.9

5 months ago

5.2.8

5 months ago

5.2.7

7 months ago

5.2.6

1 year ago

5.2.5

1 year ago

5.2.4

1 year ago

5.2.3

1 year ago

5.2.2

2 years ago

5.2.1

2 years ago

5.2.0

2 years ago

5.1.0

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

4.1.0

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

4.1.2

3 years ago

4.2.0

3 years ago

4.1.1

3 years ago

4.0.2

3 years ago

2.5.0

3 years ago

2.5.2

3 years ago

2.5.1

3 years ago

3.0.0

3 years ago

2.3.0

3 years ago

2.2.0

3 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.0.4

3 years ago

2.1.0

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.5.0

3 years ago

1.4.11

3 years ago

1.4.10

3 years ago

1.4.9

3 years ago

1.4.6

4 years ago

1.4.8

4 years ago

1.4.7

4 years ago

1.4.5

4 years ago

1.4.4

4 years ago

1.4.3

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago