score:0

Accepted answer

httpServer.listen is asynchronous, so you need to tell Jest to wait until the server starts before running test cases:

beforeAll((done) => { // Jest will wait until `done` is called
    httpServer.listen('4000', () => {
        console.log('listening on 4000');
        io.on('connection', (socket) => {
            hasConnected = true;

            socket.emit('message', sampleMessage);

            socket.on('message', (message) => {
                ...
            });
        });
        done();

    });
});

it('should connect to socket', () => {
    // Connect to localhost:4000 here
});


Related Query

More Query from same tag