title: mocha/express/supertest: 'Error: done() called multiple times'
body: <p>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.</p>
  
  <p>Here is the relevant code</p>
  
  <pre><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();
          }
      });
  });
  </code></pre>
  
  <p>My app.js has this:</p>
  
  <p><code>app.get('/', routes.index);</code></p>
  
  <p>My routes.index looks like this:</p>
  
  <pre><code>exports.index = function(req, res){
      if(!req.body.username) {
          res.redirect('/login');
      }
      res.render('index', { title: 'Express' });
  };
  </code></pre>
  
  <p>and my login page is :</p>
  
  <pre><code>exports.login = function(req,res) {
      res.render('login',{title: 'Please login'});
  };
  </code></pre>
  
  <p>I get the following error when I run the test, which makes me think that it's actually running more than once:</p>
  
  <pre><code>  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.&lt;anonymous&gt; (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.&lt;anonymous&gt; (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.&lt;anonymous&gt; (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.&lt;anonymous&gt; (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
  </code></pre>
  
  <p>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:
  <a href="https://github.com/visionmedia/supertest/issues/11#issuecomment-20251424" rel="nofollow">https://github.com/visionmedia/supertest/issues/11#issuecomment-20251424</a>
  and the test suite used by express:
  <a href="https://github.com/visionmedia/express/blob/master/test/res.redirect.js" rel="nofollow">https://github.com/visionmedia/express/blob/master/test/res.redirect.js</a>
  I'm at a loss here.
  Thanks</p>