I am trying to test my routes in express using mocha and supertest. Unfortunately I can't seem to be able to test my redirects.
Here is the relevant code
var should = require('should');
var request = require('supertest');
var app = require('../app');
describe('GET /',function() {
it('should redirect to the login screen',function(done) {
request(app)
.get('/')
.end(function(err,res) {
if(err) {
console.log("ERR: "+err);
done(err);
} else {
console.log("RES:\n",res);
res.headers.should.have.property('location','/login');
done();
}
});
});
My app.js has this:
app.get('/', routes.index);
My routes.index looks like this:
exports.index = function(req, res){
if(!req.body.username) {
res.redirect('/login');
}
res.render('index', { title: 'Express' });
};
and my login page is :
exports.login = function(req,res) {
res.render('login',{title: 'Please login'});
};
I get the following error when I run the test, which makes me think that it's actually running more than once:
collection router
GET /
◦ should redirect to the login screen: GET / 302 221ms - 40b
1) should redirect to the login screen
2) should redirect to the login screen
8 passing (281 ms)
2 failing
1) collection router GET / should redirect to the login screen:
Error: done() called multiple times
at multiple (development/git/ivorytower/node_modules/mocha/lib/runnable.js:175:31)
at done (/development/git/ivorytower/node_modules/mocha/lib/runnable.js:181:26)
at development/git/ivorytower/node_modules/mocha/lib/runnable.js:197:9
at development/git/ivorytower/test/routes.js:15:4
at Test.assert (development/git/ivorytower/node_modules/supertest/lib/test.js:190:3)
at development/git/ivorytower/node_modules/supertest/lib/test.js:119:10
at Test.Request.callback (development/git/ivorytower/node_modules/supertest/node_modules/superagent/lib/node/index.js:573:30)
at Test.<anonymous> (development/git/ivorytower/node_modules/supertest/node_modules/superagent/lib/node/index.js:133:10)
at Test.EventEmitter.emit (events.js:95:17)
at IncomingMessage.<anonymous> (development/git/ivorytower/node_modules/supertest/node_modules/superagent/lib/node/index.js:703:12)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:910:16
at process._tickCallback (node.js:415:13)
2) collection router GET / should redirect to the login screen:
Error: done() called multiple times
at multiple (development/git/ivorytower/node_modules/mocha/lib/runnable.js:175:31)
at done (development/git/ivorytower/node_modules/mocha/lib/runnable.js:181:26)
at development/git/ivorytower/node_modules/mocha/lib/runnable.js:197:9
at development/git/ivorytower/test/routes.js:15:4
at Test.assert (development/git/ivorytower/node_modules/supertest/lib/test.js:190:3)
at development/git/ivorytower/node_modules/supertest/lib/test.js:119:10
at Test.Request.callback (development/git/ivorytower/node_modules/supertest/node_modules/superagent/lib/node/index.js:573:30)
at Test.<anonymous> (development/git/ivorytower/node_modules/supertest/node_modules/superagent/lib/node/index.js:133:10)
at Test.EventEmitter.emit (events.js:95:17)
at IncomingMessage.<anonymous> (development/git/ivorytower/node_modules/supertest/node_modules/superagent/lib/node/index.js:703:12)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:910:16
at process._tickCallback (node.js:415:13)
npm ERR! weird error 2
npm ERR! not ok code 0
I have looked and found some possibly related bugs, but they're almost a year old and I am following the suggested workarounds to no avail: https://github.com/visionmedia/supertest/issues/11#issuecomment-20251424 and the test suite used by express: https://github.com/visionmedia/express/blob/master/test/res.redirect.js I'm at a loss here. Thanks
The issue was my router code:
exports.index = function(req, res){
if(!req.body.username) {
res.redirect('/login');
}
res.render('index', { title: 'Express' });
};
should have been
exports.index = function(req, res){
if(!req.body.username) {
res.redirect('/login');
} else {
res.render('index', { title: 'Express' });
}
};
So the response was getting a redirect written to it, then immediately afterwards was having a template rendered to it (if I am understanding it correctly). The if/then/else block (or a return in the if block) fixed the problem.