title: Mocha tests running forever
body: <p>Hello all and thanks for the attention.</p>
  
  <p>I'm trying to run tests with mocha and supertest, and, even though things work, the tests run forever. To avoid this, I added one "process.exit(0)" in the after() method, so it can build correctly, but this seems really wrong for "isolation" (besides, it looks bad :-) )</p>
  
  <p>My package.json:</p>
  
  <pre><code>{
    "name": "application-name",
    "version": "0.0.1",
    "private": true,
    "scripts": {
      "start": "nodejs ./bin/www",
      "test": "mocha"
    },
    "dependencies": {
      "body-parser": "~1.0.0",
      "cookie-parser": "~1.0.1",
      "debug": "~0.7.4",
      "ejs": "~0.8.5",
      "express": "~4.0.0",
      "is-my-json-valid": "^2.16.1",
      "knex": "^0.13.0",
      "morgan": "~1.0.0",
      "mysql": "^2.14.1",
      "static-favicon": "~1.0.0"
    },
    "devDependencies": {
      "mocha": "^4.0.0",
      "supertest": "^3.0.0",
      "chai": "^4.1.2",
      "util": "^0.10.3"
    }
  }
  </code></pre>
  
  <p>My test:</p>
  
  <pre><code>var request = require('supertest');
  var util    = require('util');
  
  describe('Endpoints', function () {
    var server;
  
    beforeEach(function() {
      server = require('../app').listen(3000);
    });
  
    afterEach(function(done) {
      server.close(done);
    });
  
    after(function(done){
      process.exit(0); // &lt;-- THIS LOOKS REALLY BAD
    });
  
    it('Success ', function(done) {
      var deputy = {name:"Meu Nome", uf:"SP", site_id:"1", secondary_site_id:"2"}
      request(server)
        .post('/api/deputy')
        .send(deputy)
        .expect(200, done);
    });
  
    it('Bad request ', function(done) {
      var deputy = {naonome:"Nao deve funcionar"}
      request(server)
        .post('/api/deputy')
        .send(deputy)
        .expect(400, done);
    });
  
  });
  </code></pre>
  
  <p>Any ideas why the tests never end?</p>
  
  <p>thank you all</p>
  
  <p>EDIT: follow my app.js, generated by express generator</p>
  
  <pre><code>var express = require('express');
  var path = require('path');
  var favicon = require('static-favicon');
  var logger = require('morgan');
  var cookieParser = require('cookie-parser');
  var bodyParser = require('body-parser');
  
  var routes = require('./routes/index');
  var deputy = require('./routes/deputy');
  
  var db  = require('./server/db');
  var app = express();
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'ejs');
  
  app.use(favicon());
  app.use(logger('dev'));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded());
  app.use(cookieParser());
  app.use(express.static(path.join(__dirname, 'public')));
  
  app.use('/', routes);
  app.use('/api/deputy', deputy);
  
  /// catch 404 and forwarding to error handler
  app.use(function(req, res, next) {
      var err = new Error('Not Found');
      err.status = 404;
      next(err);
  });
  
  /// error handlers
  
  // development error handler
  // will print stacktrace
  if (app.get('env') === 'development') {
      app.use(function(err, req, res, next) {
          res.status(err.status || 500);
          res.render('error', {
              message: err.message,
              error: err
          });
      });
  }
  
  // production error handler
  // no stacktraces leaked to user
  app.use(function(err, req, res, next) {
      res.status(err.status || 500);
      res.render('error', {
          message: err.message,
          error: {}
      });
  });
  
  
  module.exports = app;
  </code></pre>