A javascript mocking library for websockets. This library aims to make testing websocket applications in the bowser or phantomjs as simple as possible.
bower install mock-socket
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);
});
git clone [email protected]:thoov/mock-socket.git
cd mock-socket
npm i
npm run build
Simply run:
npm test
NOTE: that this only works in PhantomJS 2.0+.
npm run browser
Then visit: http://localhost:7357/ in your browser.
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.
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.