/mock-socket

Javascript mocking library used to simplify the testing of websocket applications in the browser, nodejs, or phantomjs.

Primary LanguageJavaScriptMIT LicenseMIT

Mock Socket

A javascript mocking library for websockets. This library aims to make testing websocket applications in the bowser or phantomjs as simple as possible.

Build Status Code Climate npm version bower version

Installation

bower install mock-socket

Example

function Chat() {
  var chatSocket = new WebSocket('ws://localhost:8080');
  this.messages = [];

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

test('basic test', function(){
  assert.expect(1);
  var done = assert.async();

  var mockServer = new MockServer('ws://localhost:8080');
  mockServer.on('connection', function(server) {
    mockServer.send('test message 1');
    mockServer.send('test message 2');
  });

  window.WebSocket = MockWebSocket;
  var chatApp = new Chat(); // Now when Chat tries to do new WebSocket it will create a MockWebSocket object

  setTimeout(function() {
    assert.equal(chatApp.messages.length, 2, '2 test messages where sent from the mock server');
    done();
  }, 100);
});

Building from source

git clone git@github.com:thoov/mock-socket.git
cd mock-socket
npm i
npm run build

Running tests

1) Via PhantomJS 2.0+

Simply run:

npm test

NOTE: that this only works in PhantomJS 2.0+.

2) Via the browser

  npm run browser

Then visit: http://localhost:7357/ in your browser.

3) Manual tests

The point of the manual tests are to compare a MockWebSocket object vs the native WebSocket object. Running the below command brings up a blank webpage that has both a MockWebSocket object and a Native WebSocket object define with debuggers in place so you can quickly start debugging any inconsistencies.

  npm start

Then visit: http://localhost:4200 in your browser.

Feedback / Issues

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.