Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually trigger a net.socket error event during testing

I wrote a TCP server using Node.js and on my tests (with Mocha) I'm testing that the server actually emits all the events it should. The one problem that I'm finding is that I cannot trigger the error event at will so I cannot automate this test.

    socket.on('error', function()
    {
        // How do I test this?
    });

Is there a way to trigger this event manually? O maybe craft a corrupt packet?

like image 662
Julian Avatar asked Dec 19 '25 05:12

Julian


1 Answers

You can emit it manually: socket.emit('error', new Error('foo bar baz'));

like image 58
mscdex Avatar answered Dec 20 '25 21:12

mscdex