Registry / testing / websocket-mockup

websocket-mockup

JSON →
library1.3.1jsnpmunverified

websocket-mockup is a lightweight mock WebSocket server and client implementation for testing, supporting both server-side and W3C WebSocket API shims. Version 1.3.1 is the latest stable release, with infrequent updates. It allows developers to simulate WebSocket connections without a real server, ideal for unit and integration tests. Key differentiators include simple API, event-driven request handling, and support for custom host routing. Unlike heavier solutions like mock-socket, this package focuses on mimicking the Node.js ws library server interface.

npm install websocket-mockup
INSTALL
IMPORT
SIG · WEBSOCKET-MOCKUP
W
websocket-mockup
testingjavascriptv1.3.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

WebSocketServer
const { WebSocketServer } = require('websocket-mockup')
import { WebSocketServer } from 'websocket-mockup'
Package does not provide ESM exports; CommonJS require only.
WebSocket
const { WebSocket } = require('websocket-mockup')
const WebSocket = require('websocket-mockup')
Must destructure; default export is an object containing both classes.
default import
const mockup = require('websocket-mockup'); const { WebSocketServer } = mockup;
import mockup from 'websocket-mockup'
No default export; TypeError will occur.

Creates a mock WebSocket server and client, demonstrates message exchange and event handling.

const { WebSocketServer, WebSocket } = require('websocket-mockup'); const server = new WebSocketServer({ host: 'test.local' }); server.on('request', (request) => { const connection = request.accept(request.requestedProtocols[0], request.origin); connection.on('message', (msg) => { if (msg.type === 'utf8') connection.sendUTF(msg.utf8Data); connection.close(); }); }); const ws = new WebSocket('wss://test.local'); ws.onopen = () => ws.send('Hello'); ws.onmessage = (event) => console.log(event.data);
Debug
Known issues
gotchaWebSocket constructor does not implement connection timeout; tests may hang if server not set up.
fix
Ensure server is listening before creating client, or use a timeout in test environment.
affects: >=1.0.0
gotchaEvent names are lowercase: 'onopen', 'onmessage', not 'open', 'message'.
fix
Use property assignment (ws.onopen = handler) or addEventListener if shimmed.
affects: >=1.0.0
gotcharequest.accept() must be called synchronously inside the 'request' event listener; async calls may fail.
fix
Move await logic outside the event handler.
affects: >=1.0.0
gotchaBinary messages use ArrayBuffer; TextEncoder/TextDecoder needed for text/binary conversion.
fix
Wrap binaryData in Buffer or decode with TextDecoder as needed.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Class constructor WebSocketServer cannot be invoked without 'new'
Using decorator or call expression without 'new' in strict mode.
fix
Add 'new': new WebSocketServer({ ... }).
TypeError: Cannot read properties of undefined (reading 'accept')
Accessing request.accept before server starts listening or request not received.
fix
Ensure server is created and 'request' event fires; use async/await with server setup.
Error: connect ECONNREFUSED 127.0.0.1:80
Attempting to connect to a real WebSocket server instead of mock (URL not matched).
fix
Use a URL with host matching server's host option, e.g., 'wss://test.local'.
Upgrade
Version history
1.3.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
14
Resources
websocket-mockup — npm install websocket-mockup · libregistry