Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-websocket-mock

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-websocket-mock

Mock websockets and assert complex websocket interactions with Jest

  • 2.5.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
139K
decreased by-18.1%
Maintainers
1
Weekly downloads
 
Created

What is jest-websocket-mock?

The jest-websocket-mock package is a testing utility for mocking WebSocket connections in Jest tests. It allows developers to simulate WebSocket servers and clients, making it easier to test real-time applications without needing a live WebSocket server.

What are jest-websocket-mock's main functionalities?

Mock WebSocket Server

This feature allows you to create a mock WebSocket server that can be used in your tests. The server can be started and stopped as needed.

const WS = require('jest-websocket-mock');

const server = new WS('ws://localhost:1234');

// Your test code here

server.close();

Mock WebSocket Client

This feature allows you to create a mock WebSocket client that can connect to the mock server. This is useful for testing client-side WebSocket logic.

const WS = require('jest-websocket-mock');

const server = new WS('ws://localhost:1234');
const client = new WebSocket('ws://localhost:1234');

client.onopen = () => {
  console.log('Client connected');
};

server.on('connection', socket => {
  console.log('Server received connection');
});

Simulate Server Messages

This feature allows the mock server to send messages to the client, simulating real server-client communication. This is useful for testing how the client handles incoming messages.

const WS = require('jest-websocket-mock');

const server = new WS('ws://localhost:1234');
const client = new WebSocket('ws://localhost:1234');

client.onmessage = event => {
  console.log('Client received message:', event.data);
};

server.send('Hello, client!');

Simulate Client Messages

This feature allows the mock client to send messages to the server, simulating real client-server communication. This is useful for testing how the server handles incoming messages.

const WS = require('jest-websocket-mock');

const server = new WS('ws://localhost:1234');
const client = new WebSocket('ws://localhost:1234');

server.on('message', message => {
  console.log('Server received message:', message);
});

client.onopen = () => {
  client.send('Hello, server!');
};

Other packages similar to jest-websocket-mock

Keywords

FAQs

Package last updated on 10 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc