New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@relaycorp/ws-mock

Package Overview
Dependencies
Maintainers
2
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@relaycorp/ws-mock

Mock client and server to unit test the NPM package 'ws'

  • 1.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.6K
increased by5.57%
Maintainers
2
Weekly downloads
 
Created
Source

@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 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.

Using the mock server

Simply initialise MockServer with the ws client connection you wish to test. For example:

  test('Server message should be played back', async () => {
    const mockConnection = new MockConnection();
    const mockServer = new MockServer(mockConnection);
    const clientUnderTest = new ClientUnderTest(mockConnection);

    clientUnderTest.connectToServer();
    mockServer.send('foo');

    const clientResponse = await mockServer.receive();
    expect(clientResponse).toEqual('foo');
  });

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);

FAQs

Package last updated on 15 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc