Search code examples
node.jsexpressunit-testing

How to test Connect / Express middleware?


Supposed I have a middleware for Connect and / or Express. What's the best way to unit test this middleware?

Of course, I can set up an http server in the unit tests, and load the middleware into this. But supposed that I want to test startup behavior, I need several http servers, which soon gets complicated and unclear (think of using a new port in each single test).

Is there a better way, or is testing it inside a real server the best one can come up with?


Solution

  • supertest does the job, as pointed out by SLaks in the comments.