How to track down ECONNREFUSED in a Node.js TCP server?

I have written a TCP server using dnode. Basically, everything works fine.

Now I have also created some unit tests, and one of them fails with ECONNREFUSED.

What's strange about this is that

  • the code being tested works great when run outside the unit test, inside the application,
  • the code being tested works if I comment out the test before the failing test.

So obviously there seems to be an interdependency between two tests. Both work alone, but not together with each other.

The problem is that I get a quite useless error message:

Error: connect ECONNREFUSED
    at errnoException (net.js:768:11)
    at Object.afterConnect [as oncomplete] (net.js:759:19)

It's nice to see that it's an ECONNREFUSED error, but how do I figure out what is causing the issue?

As there are lots of callbacks who incorporate anonymous functions it is not that easy to figure out which line causes the issues :-(.

Any ideas / hints?

I solved it by myself, see the comments above.