Testing Middleware

Testing Middleware

Recently I wrote a small custom piece of middleware for an express application to consume on its requests. It takes a query parameter and reworks it for later use in the event loop. Nothing too fancy or overly complicated.

But as is the good practice, and because this was for a production application, I wanted to test this bit of middleware with mocha. But how?

A brief google search did not return to me any relevant information, so that's what prompted this post.

Writing Middleware

Middleware works off of a promise event loop.  When a task is completed, its expected that something tell the framework to move on to the next event in the event loop. In express, and indeed with most node frameworks, middleware assumes that are going to pass in a minimum three arguments (request, response, and next), and that the last argument is the callback or the promise which will tell the framework to fire its next event.

Testing Middleware

But how do we test the middleware?  Typical mocha with supertest testing makes a call to your application, and evaluates the resulting response.  This isnt what we want to do in this case. We want to test the individual isolated middleware.

What I found to be good result was to manually call the middleware itself and pass in request, response, and next. Fortunately, there is already a module on npm called node-mocks-http which can be used to quickly setup both request and response for us to pass into our middleware.

With request and response mock'd and ready to pass in, the question is how do we test the result of our middleware.  The answer to that is to pass in a callback function as next into the middleware.

Because middleware calls the next function by default (or at least it should be), by passing in a function as the next argument, we are telling the system run our callback instead of running the next item in what would be the event loop. This means that we can run our actual tests in the next callback, and have access to all the resulting changes to our request and response global variables.

I have written a quick gist to demonstrate this in action.

Conclusion

With mocha testing able to run individual tests on middleware, there is absolutely no need for overly complicated testing schemes for your node applications. Middleware makes it easy to inject manageable blocks of code into your event loop make bugs easier to find, your code more easily tested, and of course, more enjoyable to work it.