Gathering detailed insights and metrics for mock-socket
Gathering detailed insights and metrics for mock-socket
Gathering detailed insights and metrics for mock-socket
Gathering detailed insights and metrics for mock-socket
@omghax/ember-mock-socket-shim
Ember shim for mock-socket
hot-shots
Node.js client for StatsD, DogStatsD, and Telegraf
mock-socket-with-protocol
Javascript mocking library for websockets and socket.io, compatible with apollographql subscriptions-transport-ws
mock-http
mock http request response
Javascript mocking library for WebSockets and Socket.IO
npm install mock-socket
Min node v8
Published on 29 Jul 2019
Revert node version change
Published on 29 Jul 2019
8.1.0
Published on 24 Jul 2019
Fix cannot find name 'USVString' in Typescript
Published on 30 Oct 2018
More typescript improvements
Published on 08 Oct 2018
Typescript improvements
Published on 26 Jul 2018
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
800 Stars
545 Commits
119 Forks
9 Watching
7 Branches
44 Contributors
Updated on 30 Oct 2024
Minified
Minified + Gzipped
JavaScript (99.82%)
HTML (0.18%)
Cumulative downloads
Total Downloads
Last day
-6.5%
82,252
Compared to previous day
Last week
3%
460,914
Compared to previous week
Last month
14%
1,845,683
Compared to previous month
Last year
-17.1%
21,706,684
Compared to previous year
Javascript mocking library for websockets and socket.io
1npm install mock-socket
1import { WebSocket, Server } from 'mock-socket';
1import test from 'ava'; 2import { Server } from 'mock-socket'; 3 4class ChatApp { 5 constructor(url) { 6 this.messages = []; 7 this.connection = new WebSocket(url); 8 9 this.connection.onmessage = event => { 10 this.messages.push(event.data); 11 }; 12 } 13 14 sendMessage(message) { 15 this.connection.send(message); 16 } 17} 18 19test.cb('that chat app can be mocked', t => { 20 const fakeURL = 'ws://localhost:8080'; 21 const mockServer = new Server(fakeURL); 22 23 mockServer.on('connection', socket => { 24 socket.on('message', data => { 25 t.is(data, 'test message from app', 'we have intercepted the message and can assert on it'); 26 socket.send('test message from mock server'); 27 }); 28 }); 29 30 const app = new ChatApp(fakeURL); 31 app.sendMessage('test message from app'); // NOTE: this line creates a micro task 32 33 // NOTE: this timeout is for creating another micro task that will happen after the above one 34 setTimeout(() => { 35 t.is(app.messages.length, 1); 36 t.is(app.messages[0], 'test message from mock server', 'we have stubbed our websocket backend'); 37 mockServer.stop(t.done); 38 }, 100); 39});
1import { WebSocket, Server } from 'mock-socket'; 2 3/* 4 * By default the global WebSocket object is stubbed out when 5 * a new Server instance is created and is restored when you stop 6 * the server. 7 * However, you can disable this behavior by passing `mock: false` 8 * to the options and manually mock the socket when you need it. 9 */ 10const server = new Server('ws://localhost:8080', { mock: false }); 11 12/* 13 * If you need to stub something else out you can like so: 14 */ 15 16window.WebSocket = WebSocket; // Here we stub out the window object
1const mockServer = new Server('ws://localhost:8080'); 2 3mockServer.on('connection', socket => { 4 socket.on('message', () => {}); 5 socket.on('close', () => {}); 6 socket.on('error', () => {}); 7 8 socket.send('message'); 9 socket.close(); 10}); 11 12mockServer.clients(); // array of all connected clients 13mockServer.emit('room', 'message'); 14mockServer.stop(optionalCallback);
A declaration file is included by default. If you notice any issues with the types please create an issue or a PR!
Socket.IO has limited support. Below is a similar example to the one above but modified to show off socket.io support.
1import test from 'ava'; 2import { SocketIO, Server } from 'mock-socket'; 3 4class ChatApp { 5 constructor(url) { 6 this.messages = []; 7 this.connection = new io(url); 8 9 this.connection.on('chat-message', data => { 10 this.messages.push(event.data); 11 }); 12 } 13 14 sendMessage(message) { 15 this.connection.emit('chat-message', message); 16 } 17} 18 19test.cb('that socket.io works', t => { 20 const fakeURL = 'ws://localhost:8080'; 21 const mockServer = new Server(fakeURL); 22 23 window.io = SocketIO; 24 25 mockServer.on('connection', socket => { 26 socket.on('chat-message', data => { 27 t.is(data, 'test message from app', 'we have intercepted the message and can assert on it'); 28 socket.emit('chat-message', 'test message from mock server'); 29 }); 30 }); 31 32 const app = new ChatApp(fakeURL); 33 app.sendMessage('test message from app'); 34 35 setTimeout(() => { 36 t.is(app.messages.length, 1); 37 t.is(app.messages[0], 'test message from mock server', 'we have subbed our websocket backend'); 38 39 mockServer.stop(t.done); 40 }, 100); 41});
The easiest way to work on the project is to clone the repo down via:
1git clone git@github.com:thoov/mock-socket.git 2cd mock-socket 3yarn install
Then to create a local build via:
1yarn build
Then create a local npm link via:
1yarn link
At this point you can create other projects / apps locally and reference this local build via:
1yarn link mock-socket
from within your other projects folder. Make sure that after any changes you run yarn build
!
This project uses ava.js as its test framework. Tests are located in /tests. To run tests:
1yarn test
This project uses eslint and a rules set from airbnb's javascript style guides. To run linting:
1yarn lint
This project uses prettier. To run the formatting:
1yarn format
Code coverage reports are created in /coverage after all of the tests have successfully passed. To run the coverage:
1yarn test:coverage
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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
Found 6/13 approved changesets -- score normalized to 4
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
25 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More