How to write test driven programming in node.js?


Recently I got introduced to node.js and packages like express, mongodb and ejs. I have few questions :

As a learning purpose, I have created a github repo of user management which uses express, mongodb and ejs. I have all my functions in routes/users.js file. I need to write test cases all these functions. How to create a test driven programming with this example?

In my routes in app.js file.

app.get('/login', user.login);'/login', user.loginSubmit);

I need to write different routes to login page renders and submit etc. If there are some ajax request also, then we have lots of routes in app.js file when considering to routes of a single page. Is it like that or need to change my structure?

Problem courtesy of: Justin John


I recommend you Mocha, it's from the same guy of expressjs. It supports test coverage for you code, hooks before, after, each and of course it supports async code.

I use it in combination with should.js or even chai.js

A test in mocha looks like, the code is from my own test where I'm using superagent, in order to make requests.

it('requests a permission with valid ticket', function (done){
            .post(route.server + '/preq')
            .set('Content-Type', 'application/json')
            .set('Authorization', 'Bearer ' + ACCESSTOKEN)
            .send({ticket: TICKET})
            .end(function (req,res) {

                var location = .....
Solution courtesy of: Maldo


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.