Is there any way to pass an existing socket.io instance to mock-socket? (get all event listeners / handlers / callbacks and pass them to mock-socket)
So I'm sorta new to testing and I still haven't wrapped my head around mocks very much..
Right now I have a class which uses a library called socket-controllers.
It allows you to write socket.io events using decorators.
Here's my example class:
@SocketController()
export class OrderController {
private productRepository: Repository<Product>;
/**
* Socket Emit place-order
*
* Places an order based on the socket's message body
* @param socketId
* @param order
*/
@OnMessage('place-order')
async place(@SocketId() socketId: string, @MessageBody() order: Order) {
// some code...
io.emit('placed-order-admin', orders);
io.to(order.customerId).emit('placed-order', order);
}
}
And in order to attach all of those class methods you pass an existing socket.io instance to the library, like this:
import createSocketIoServer from 'socket.io';
import { app } from './express';
const server = http.createServer(app);
const io = createSocketIoServer(server);
useSocketServer(io, {
controllers: [`${__dirname}/controllers/*.io.ts`]
});
And so basically right now I'm trying to test this using jest. However I'm running into issues making a the connection between the socket.io client and the socket.io server. So that's how I found mock-socket. Right now I'm trying to figure out how to use it exactly and is it possible to somehow pass an existing io so that your library can get all of the event listeners without me having to write them all over again. I don't know if my explanation is clear enough :/. I'd be glad if I get some explanation on how to use this / if I'm doing it the right way.