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.
Key facts
Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.
| Field | Value |
|---|---|
| Repository | thoov/mock-socket |
| Owner | thoov |
| Primary language | JavaScript |
| License | MIT — OSI-approved |
| Stars | 805 |
| Forks | 123 |
| Open issues | 75 |
| Latest release | 9.0.0 (2019-07-29) |
| Last updated | 2025-02-10 |
| Source | https://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.
Get the mock-socket source
Clone the repository and explore it locally.
git clone https://github.com/thoov/mock-socket.gitcd mock-socket# follow the project's README for install & configurationNeed it deployed, integrated, or customized instead? DEV.co ships production installs.
Best use cases
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.
| Signal | Assessment |
|---|---|
| Maintenance | Moderate |
| Documentation | Adequate |
| License clarity | Clear |
| Deployment complexity | Low |
| DEV.co fit | Good |
| Assessment confidence | High |
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.
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.coRelated 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?
Does mock-socket support Socket.IO?
Will mock-socket work in Node.js tests?
Is mock-socket safe for production use?
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.