✅ How to Mock socket.io
in Jest Like a Senior Developer
If you're writing unit tests in a Node.js application that uses socket.io
, you've probably hit the "how do I mock this real-time madness?" wall. Fret not—this article is your guide to mocking socket.io
with Jest like a pro.
🎯 Why Mock socket.io
?
socket.io
is great for real-time communication, but when it comes to testing, you don’t want actual WebSocket connections firing up during your test suite. That’s where mocking comes in:
- ✅ Avoid real network calls
- ✅ Speed up your tests
- ✅ Isolate logic for unit testing
Let’s break down how to mock socket.io
on both client and server side.
📦 Prerequisites
Make sure you have the following installed:
npm install --save-dev jest
npm install socket.io
🧪 Scenario: Mocking socket.io
Server in Jest
Imagine you have a server that looks like this:
// server.js
const { Server } = require('socket.io');
const http = require('http');
const server = http.createServer();
const io = new Server(server);
io.on('connection', (socket) => {
console.log('A user connected');
socket.on('ping', () => {
socket.emit('pong');
});
});
module.exports = server;
Now you want to write unit tests that mock socket.io.Server
.
🧰 How to Mock socket.io.Server
with Jest
Step 1: Mock the Module
// __mocks__/socket.io.js
const mSocket = {
on: jest.fn(),
emit: jest.fn(),
};
const mServer = {
on: jest.fn(),
};
const Server = jest.fn(() => mServer);
module.exports = {
Server,
__mocks__: {
mServer,
mSocket,
},
};
Step 2: Tell Jest to Use the Mock
jest.mock('socket.io');
Step 3: Write Your Test
// server.test.js
const http = require('http');
const { Server, __mocks__ } = require('socket.io');
const server = require('./server');
describe('Socket.IO Server', () => {
it('should initialize the socket server and listen to connections', () => {
expect(Server).toHaveBeenCalledWith(expect.any(http.Server));
expect(__mocks__.mServer.on).toHaveBeenCalledWith('connection', expect.any(Function));
});
});
Boom 💥 — you've now mocked socket.io.Server
and verified that your server code registers connection listeners.
🧑💻 Bonus: Mocking socket.io-client
in Jest
If you’re testing client-side code with socket.io-client
, follow the same pattern:
// __mocks__/socket.io-client.js
const mSocket = {
on: jest.fn(),
emit: jest.fn(),
};
const io = jest.fn(() => mSocket);
module.exports = io;
module.exports.__mocks__ = { mSocket };
Then in your test:
jest.mock('socket.io-client');
const io = require('socket.io-client');
const { __mocks__ } = require('socket.io-client');
test('Client should emit event', () => {
const socket = io();
socket.emit('ping');
expect(__mocks__.mSocket.emit).toHaveBeenCalledWith('ping');
});
🧠 Pro Tips
- For full control, consider using
EventEmitter
in your mock to simulate events. - Separate integration tests from unit tests—don’t mix mocks with real sockets.
- Use
socket.io-client
'sio()
mock to simulate connections in frontend tests.
📚 TL;DR
Task | How-To |
---|---|
Mock socket.io.Server | Create __mocks__/socket.io.js and mock Server |
Mock client socket | Create __mocks__/socket.io-client.js |
Use in Jest test | jest.mock('socket.io') or jest.mock('socket.io-client') |
🔚 Conclusion
Mocking socket.io
in Jest doesn’t have to be rocket science. With the right setup, you can test your real-time code without spinning up a single WebSocket. Mock smart, test fast 🚀.
Looking for more Node.js testing tips? Bookmark this blog and stay tuned!