Testing socket.io apps

Socket.io is probably one of the coolest things to come out of the JavaScript world in recent years. Finally! Something that lets web developers create real-time apps without the fuss of thinking about websockets and long polling and all the other hacks that need to be used.

The idea is pretty simple, the server can emit an event and the client will pick it up. The converse also happens. Callbacks through the server-client barrier works as well. Socket.io takes care of deciding which of the real-time hacks should be used to make the magic happen.

Thing is, the interplay of client and server makes socket.io apps a bit difficult to test.

Disconnecting clients after tests is very important. As I’ve discovered, not disconnecting can lead to the socket accumulating event listeners, which in turn can fire completely different tests than what you expect. It also leads to tests that pass 70% of the time, but fail in random ways.