Unittesting and mocking nodeJS net/Socket events

I'm TDD'ing a script that connects to a Socket from the net module.

For testing I use mocha and for mocking sinonjs

Now I'm really having problem mocking/faking the data-emitting of the Socket.

e.g.: I want to test this code:

socket.on('data', function (data) {
    console.log(data);
}

How can I fake an emit of 'data'?

You will want to expose the socket variable to the test somehow. I'd need to see more of your code to recommend the best way to do that.

And then in your test, you can call socket.emit().