5

Hello all and thanks for the attention.

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 :-) )

My package.json:

{
  "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"
  }
}

My test:

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); // <-- 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);
  });

});

Any ideas why the tests never end?

thank you all

EDIT: follow my app.js, generated by express generator

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;
Allan Vital
  • 376
  • 4
  • 18

2 Answers2

8

Pass --exit to the mocha process.

mocha --exit

At version 4.0.0, Mocha changed the default behavior from exiting after tests were complete to not exiting. From that point on, you must add the --exit flag if you want mocha to stop when tests have been run.

https://github.com/mochajs/mocha/blob/d69bf14a860235e7ceb50a50c2072dddd1ef8520/CHANGELOG.md#400--2017-10-02

Note that without the exit flag, some continuous integration systems may run forever; and that may not be desirable if you are paying for CI minutes.

blong
  • 2,815
  • 8
  • 44
  • 110
Jag Dhillon
  • 342
  • 4
  • 5
  • Thanks for your answer! I recommend giving a simple explanation about *why* this fixes the problem, and what OP was doing wrong with his code. One or two sentences is usually sufficient. – Lord Elrond Mar 16 '19 at 21:56
3

Sounds like the database connection is still running. You can close it with

connection.end(function(err) {
  // The connection is terminated now
});

I assume you can get the connection from the /server/db file. Export it if you don't do that already.

I don't use mysql myself, but this is what my after functions looks like when using MongoDB:

after(function (done) {
    server.close(function () {
      mongoose.connection.close(done)
    })
})

If I don't close the database connection the tests will run all the way to the end, but never finish and exit to the shell.

Mika Sundland
  • 18,120
  • 16
  • 38
  • 50
  • 1
    You are right. Instead of mongoose I am using knex, so in the "after" bit, it was knex.destroy(), instead of mongoose.connection.close(). – Allan Vital Oct 16 '17 at 13:31