Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

mock-socket

thoov1.1mMIT9.3.1TypeScript support: included

Javascript mocking library for websockets and socket.io

websockets, mock, mocksocket, sockets

readme

Javascript mocking library for websockets and socket.io

Build Status

Contents

Installation

npm install mock-socket
import { WebSocket, Server } from 'mock-socket';

Usage

import test from 'ava';
import { Server } from 'mock-socket';

class ChatApp {
  constructor(url) {
    this.messages = [];
    this.connection = new WebSocket(url);

    this.connection.onmessage = event => {
      this.messages.push(event.data);
    };
  }

  sendMessage(message) {
    this.connection.send(message);
  }
}

test.cb('that chat app can be mocked', t => {
  const fakeURL = 'ws://localhost:8080';
  const mockServer = new Server(fakeURL);

  mockServer.on('connection', socket => {
    socket.on('message', data => {
      t.is(data, 'test message from app', 'we have intercepted the message and can assert on it');
      socket.send('test message from mock server');
    });
  });

  const app = new ChatApp(fakeURL);
  app.sendMessage('test message from app'); // NOTE: this line creates a micro task

  // NOTE: this timeout is for creating another micro task that will happen after the above one
  setTimeout(() => {
    t.is(app.messages.length, 1);
    t.is(app.messages[0], 'test message from mock server', 'we have stubbed our websocket backend');
    mockServer.stop(t.done);
  }, 100);
});

Advanced Usage

Stubbing the "global"

import { WebSocket, Server } from 'mock-socket';

/*
 * By default the global WebSocket object is stubbed out when 
 * a new Server instance is created and is restored when you stop
 * the server.
 * However, you can disable this behavior by passing `mock: false`
 * to the options and manually mock the socket when you need it.
 */
const server = new Server('ws://localhost:8080', { mock: false });

/*
 * If you need to stub something else out you can like so:
 */

window.WebSocket = WebSocket; // Here we stub out the window object

Server Methods

const mockServer = new Server('ws://localhost:8080');

mockServer.on('connection', socket => {
  socket.on('message', () => {});
  socket.on('close', () => {});
  socket.on('error', () => {});

  socket.send('message');
  socket.close();
});

mockServer.clients(); // array of all connected clients
mockServer.emit('room', 'message');
mockServer.stop(optionalCallback);

Typescript Support

A declaration file is included by default. If you notice any issues with the types please create an issue or a PR!

Socket IO

Socket.IO has limited support. Below is a similar example to the one above but modified to show off socket.io support.

import test from 'ava';
import { SocketIO, Server } from 'mock-socket';

class ChatApp {
  constructor(url) {
    this.messages = [];
    this.connection = new io(url);

    this.connection.on('chat-message', data => {
      this.messages.push(event.data);
    });
  }

  sendMessage(message) {
    this.connection.emit('chat-message', message);
  }
}

test.cb('that socket.io works', t => {
  const fakeURL = 'ws://localhost:8080';
  const mockServer = new Server(fakeURL);

  window.io = SocketIO;

  mockServer.on('connection', socket => {
    socket.on('chat-message', data => {
      t.is(data, 'test message from app', 'we have intercepted the message and can assert on it');
      socket.emit('chat-message', 'test message from mock server');
    });
  });

  const app = new ChatApp(fakeURL);
  app.sendMessage('test message from app');

  setTimeout(() => {
    t.is(app.messages.length, 1);
    t.is(app.messages[0], 'test message from mock server', 'we have subbed our websocket backend');

    mockServer.stop(t.done);
  }, 100);
});

Contributing

The easiest way to work on the project is to clone the repo down via:

git clone git@github.com:thoov/mock-socket.git
cd mock-socket
yarn install

Then to create a local build via:

yarn build

Then create a local npm link via:

yarn link

At this point you can create other projects / apps locally and reference this local build via:

yarn link mock-socket

from within your other projects folder. Make sure that after any changes you run yarn build!

Tests

This project uses ava.js as its test framework. Tests are located in /tests. To run tests:

yarn test

Linting

This project uses eslint and a rules set from airbnb's javascript style guides. To run linting:

yarn lint

Formatting

This project uses prettier. To run the formatting:

yarn format

Code Coverage

Code coverage reports are created in /coverage after all of the tests have successfully passed. To run the coverage:

yarn test:coverage

Feedback

If you have any feedback, encounter any bugs, or just have a question, please feel free to create a github issue or send me a tweet at @thoov.

changelog

Mock Socket Changelog

v9.3.1 (Sep 11th, 2023)

  • #383 [BUGFIX] Calling close in the CONNECTING state should not cause onopen to be called #383

v9.3.0 (Sep 5th, 2023)

  • #382 The send() method now only throws when the socket is in the CONNECTING state

v9.2.1 (Feb 14th, 2023)

  • #376 Do not normalize data when emitting to socket.io sockets

v9.2.0 (Feb 9th, 2023)

v9.1.5 (June 5th, 2022)

  • #362 [BUGFIX] Event handler getters should return a single function

v9.1.4 (May 25th, 2022)

  • #298 [BUGFIX] close listener of the socket isn't called when client close

v9.1.3 (April 20th, 2022)

  • #355 Bump url-parse from 1.5.2 to 1.5.9

v9.1.2 (January 25th, 2022)

  • #352 fix SocketIO types

v9.1.1 (January 25th, 2022)

  • #351 add types for SocketIO

v9.1.0 (January 13th, 2022)

  • #348 [BUGFIX] Address misspelling of cancelBuble
  • #349 add mock options param to prevent stubbing global

v9.0.8 (November 15th, 2021)

  • #343 trim query params for attachServer method lookup
  • #335 Update type of url parameter of websocket constructor

v9.0.7 (November 1st, 2021)

  • #342 Accessing to websocket proxy via server clients

v9.0.6 (October 18th, 2021)

  • #338 [BUGFIX] Use default codes for close event
  • #340 Build optimisations

v9.0.5 (September 30th, 2021)

  • #312 [BUGFIX] Fix null pointer exceptions
  • #296 [BUGFIX] Add the hasListeners method to client socket
  • #314, #316, #318, #321, #323, #330, #331, #332 Bump versions
  • #336 Remove src folder from npm

v9.0.4 (September 27th, 2021)

  • #240 [BUGFIX] Fixed undefined readyState for WebSockets after closing connection in Server
  • #328 [BUGFIX] Use native typed arguments for WebSocket event handlers

v9.0.2 (October 9th, 2019)

  • #285 [BUGFIX] Removing .git directory from npm package + cleanup

v9.0.1 (October 5th, 2019)

  • #281 [BUGFIX] Updating the workflow
  • #276 [BUGFIX] Export ES Module format with .mjs extension