Unit testing expressjs application
You can use Supertest
var request = require('supertest') , express = require('express');var app = express();app.get('/user', function(req, res){ res.send(200, { name: 'tobi' });});request(app) .get('/user') .expect('Content-Type', /json/) .expect('Content-Length', '20') .expect(200) .end(function(err, res){if (err) throw err; });
Using Mocha:
describe('GET /users', function(){ it('respond with json', function(done){ request(app) .get('/user') .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200, done); })})
I suggest you to divide into two file your app: the app.js that contains all your app code and return it with module.exports, and the server.js file that requires app.js and pass it to a new http server listen method.This way you can write tests doing require of app.js.
This is how the default app created with express generator work.