Gathering detailed insights and metrics for mock-socket-with-protocol
Gathering detailed insights and metrics for mock-socket-with-protocol
Gathering detailed insights and metrics for mock-socket-with-protocol
Gathering detailed insights and metrics for mock-socket-with-protocol
Javascript mocking library for websockets and socket.io
npm install mock-socket-with-protocol
Typescript
Module System
Min. Node Version
Node Version
NPM Version
78.2
Supply Chain
99.5
Quality
74.9
Maintenance
100
Vulnerability
100
License
JavaScript (99.78%)
HTML (0.22%)
Total Downloads
374,715
Last Day
14
Last Week
865
Last Month
2,996
Last Year
23,487
MIT License
2 Stars
401 Commits
1 Watchers
9 Branches
9 Contributors
Updated on Sep 22, 2022
Minified
Minified + Gzipped
Latest Version
7.1.1
Package Id
mock-socket-with-protocol@7.1.1
Unpacked Size
913.33 kB
Size
235.39 kB
File Count
35
NPM Version
5.6.0
Node Version
9.3.0
Cumulative downloads
Total Downloads
Last Day
75%
14
Compared to previous day
Last Week
25.9%
865
Compared to previous week
Last Month
48.2%
2,996
Compared to previous month
Last Year
-50.9%
23,487
Compared to previous year
Javascript mocking library for websockets and socket.io
This is basically mock-socket made to work with apollo subscriptions-transport-ws. The implementation is not perfect, which was noticed over two years ago by @brian-mann of cypress.io : https://github.com/thoov/mock-socket/issues/72 . Since two years later this architectural problem is still not fixed, I've decided to fork with the tiny change that's just enough to make it work in this specific usecase. Thanks @thoov for the amazing work on this package! I actually started writing my own websocket server implementation, when, after a half day of happy hacking I came to realization "this is nodejs.. someone had to got this to work before me", and there it was. :)
1npm install mock-socket-with-protocol --dev
To use within a node environment you can simply import or require the files directly. This option is great for phantomjs or CI environments.
1import { WebSocket, Server, SocketIO } from 'mock-socket-with-protocol'; 2 3// OR 4 5const mockServer = require('mock-socket-with-protocol').Server; 6const socketIO = require('mock-socket-with-protocol').SocketIO; 7const mockWebSocket = require('mock-socket-with-protocol').WebSocket;
1// chat.js 2function Chat() { 3 const chatSocket = new WebSocket('ws://localhost:8080'); 4 this.messages = []; 5 6 chatSocket.onmessage = (event) => { 7 this.messages.push(event.data); 8 }; 9}
1// chat-test.js 2import { Server } from 'mock-socket-with-protocol'; 3 4describe('Chat Unit Test', () => { 5 it('basic test', (done) => { 6 const mockServer = new Server('ws://localhost:8080'); 7 mockServer.on('connection', server => { 8 mockServer.send('test message 1'); 9 mockServer.send('test message 2'); 10 }); 11 12 // Now when Chat tries to do new WebSocket() it 13 // will create a MockWebSocket object \ 14 var chatApp = new Chat(); 15 16 setTimeout(() => { 17 const messageLen = chatApp.messages.length; 18 assert.equal(messageLen, 2, '2 messages where sent from the s server'); 19 20 mockServer.stop(done); 21 }, 100); 22 }); 23});
1// chat.js 2function Chat() { 3 const chatSocket = new io('http://localhost:8080'); 4 this.messages = []; 5 6 chatSocket.on('chat-message', data => { 7 this.messages.push(data); 8 }; 9}
1// chat-test.js 2import { SocketIO, Server } from 'mock-socket-with-protocol'; 3 4describe('Chat Unit Test', () => { 5 it('basic test', (done) => { 6 const mockServer = new Server('http://localhost:8080'); 7 mockServer.on('connection', server => { 8 mockServer.emit('chat-message', 'test message 1'); 9 mockServer.emit('chat-message', 'test message 2'); 10 }); 11 12 /* 13 This step is very important! It tells our chat app to use the mocked 14 websocket object instead of the native one. The great thing 15 about this is that our actual code did not need to change and 16 thus is agnostic to how we test it. 17 */ 18 window.io = SocketIO; 19 20 // Now when Chat tries to do io() or io.connect() 21 // it will use MockSocketIO object 22 var chatApp = new Chat(); 23 24 setTimeout(() => { 25 const messageLen = chatApp.messages.length; 26 assert.equal(messageLen, 2, '2 messages where sent from the server'); 27 mockServer.stop(done); 28 }, 100); 29 }); 30});
The easiest way to work on the project is to clone the repo down via:
1git clone git@github.com:TheBrainFamily/mock-socket-with-protocol.git 2cd mock-socket-with-protocol 3yarn
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-with-protocol
from within your other projects folder. Make sure that after any changes you run yarn build
!
This project uses mocha as its test framework. Tests are located in /test and have 1 of 3 file name prefixes (functional-, issue-#, or unit-).
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 with --single-quote and --print-width 120. 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
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
no SAST tool detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/30 approved changesets -- score normalized to 0
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
91 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-30
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