DEV.co
Open-Source Testing · thoov

mock-socket

mock-socket is a JavaScript testing library that allows developers to mock WebSocket and Socket.IO connections without a real server. It intercepts WebSocket calls during tests, enabling isolated unit and integration testing of real-time communication features.

Source: GitHub — github.com/thoov/mock-socket
805
GitHub stars
123
Forks
JavaScript
Primary language
MIT
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
Repositorythoov/mock-socket
Ownerthoov
Primary languageJavaScript
LicenseMIT — OSI-approved
Stars805
Forks123
Open issues75
Latest release9.0.0 (2019-07-29)
Last updated2025-02-10
Sourcehttps://github.com/thoov/mock-socket

What mock-socket is

This library provides drop-in mocks for the WebSocket API and Socket.IO client, allowing developers to create a mock Server instance that handles connection, message, and close events. It includes TypeScript definitions and automatically stubs the global WebSocket object during test execution.

Quickstart

Get the mock-socket source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/thoov/mock-socket.gitcd mock-socket# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

Unit Testing WebSocket-Dependent Components

Test chat applications, real-time dashboards, or notification systems without running a live WebSocket server. Mock the server behavior and verify client-side event handling in isolation.

Integration Testing Real-Time Features

Validate end-to-end WebSocket flows in CI/CD pipelines with predictable, repeatable mock server responses. Simulate connection lifecycle events, message ordering, and error scenarios.

Development-Time Prototyping

Build and test real-time features before the backend WebSocket server is ready. Use mock-socket to verify front-end code contracts and API message shapes early.

Implementation considerations

  • Requires manual global WebSocket stubbing or use the default auto-stub behavior; manage stub lifecycle carefully in test suites to avoid test pollution.
  • Async message handling relies on microtasks and setTimeout; ensure test timeouts and event loop behavior match production expectations (see README examples).
  • TypeScript definitions are included but maintainer requests issues/PRs for type bugs; verify types match your TS version and test setup.
  • Socket.IO support is limited; test with your specific Socket.IO version to confirm compatibility before relying on it in CI/CD.
  • Requires familiarity with test framework integration (ava.js shown in docs); may need adaptation for Jest, Mocha, or other runners.

When to avoid it — and what to weigh

  • Testing Against Real WebSocket Behavior — If you need to verify protocol-level WebSocket details, compression, or edge cases specific to production servers, a real WebSocket connection or integration tests are more appropriate.
  • Production Runtime Use — This library is strictly for testing. Do not use it in production code or runtime environments; it will mask real network failures and break user-facing features.
  • Socket.IO with Modern Features — The README explicitly documents 'limited support' for Socket.IO. If your application relies on advanced Socket.IO features (namespaces, rooms at scale, or recent Socket.IO major versions), verify compatibility before committing.
  • High-Volume Load or Stress Testing — mock-socket is not designed for performance testing or stress scenarios. For load testing real-time systems, use dedicated tools or real servers.

License & commercial use

Licensed under MIT (MIT License), a permissive OSI-approved open-source license that allows commercial use, modification, and redistribution with minimal restrictions.

MIT license permits commercial use without restriction. However, the project shows moderate maintenance (latest release July 2019, last push Feb 2025); verify stability and support maturity for production dependencies before adoption in commercial products.

DEV.co evaluation signals

Editorial assessment — not user reviews. Directional, with an explicit confidence level.

SignalAssessment
MaintenanceModerate
DocumentationAdequate
License clarityClear
Deployment complexityLow
DEV.co fitGood
Assessment confidenceHigh
Security considerations

No known security incidents documented in the provided data. As a test-only library, it does not handle real credentials or production traffic. Ensure mock data used in tests does not leak sensitive information into logs or CI artifacts. Review any third-party dependencies for vulnerabilities (not listed in provided data).

Alternatives to consider

jest-websocket-mock

Specifically designed for Jest; simpler API for Jest users and may have better Jest integration and TypeScript support out of the box.

sinon.js (with fakes/stubs)

General-purpose mocking library with lower-level WebSocket stubbing; requires more manual setup but offers fine-grained control and works across test frameworks.

Real WebSocket server (e.g., ws + Node.js in tests)

If you need true protocol compliance or advanced Socket.IO features, spin up a real server in tests; trades simplicity for accuracy and protocol realism.

Software development agency

Build on mock-socket with DEV.co software developers

Integrate mock-socket into your test suite today. For complex WebSocket or Socket.IO architectures, consult with our specialists to design a scalable testing strategy.

Talk to DEV.co

Related open-source tools

Surfaced by semantic similarity across the DEV.co open-source index.

Related on DEV.co

Explore the category and the services that help you build with it.

mock-socket FAQ

Can I use mock-socket with Jest or Mocha?
Yes, mock-socket is framework-agnostic. However, examples in the README use ava.js; integration with Jest/Mocha requires manual setup (globals, jsdom for browser APIs, etc.). Refer to your test framework docs and the library's API.
Does mock-socket support Socket.IO?
Yes, but with limited support as noted in the README. Basic emit/on patterns work; advanced features like namespaces and rooms may not be fully implemented. Test with your Socket.IO version before relying on it.
Will mock-socket work in Node.js tests?
Yes, but you may need jsdom or a similar DOM emulation layer to provide the WebSocket global. Ensure your test environment has the necessary polyfills or globals configured.
Is mock-socket safe for production use?
No. mock-socket is strictly a testing tool. Never ship it to production; it will mask real network errors and break real-time features. Use it only in test code.

Work with a software development agency

DEV.co is a software development agency delivering custom software development services to companies building on open source. Our software developers and web developers design, integrate, and ship production systems — spanning web development, APIs, AI, data, and cloud. If mock-socket is part of your open-source testing roadmap, our team can implement, customize, migrate, and maintain it.

Ready to Test Real-Time Features?

Integrate mock-socket into your test suite today. For complex WebSocket or Socket.IO architectures, consult with our specialists to design a scalable testing strategy.