gulp-mocha: Gulp-Mocha fails to exit, but Mocha does

Hi,

I am running a simple test (its failing, but that a separate issue).

Using:

"gulp-mocha": "^1.0.0",

When I run this using Gulp-Mocha (it happens on Travis as well), it does not exit the testing execution, but when I run this using Mocha, it fails, and exits back to the command prompt.

var assert = require("assert"),
    elasticsearch = require('elasticsearch'),
    client;

    client = new elasticsearch.Client();

    describe(' people', function(){

    beforeEach(function(done){

        //in Gulp Mocha this stops the test runner exiting
        //that this does not work is not this issue.
        client.deleteByQuery({
            index: 'people',
            q: '*'
        }, function (error, response) {
            done();
        });


       //If i just do done(), it will exit. in both.
       //done();

    });

    describe('add a entry into the index', function(){
        it('should add Dave as a person to the database', function(done){
            assert.equal(1,1);
        });
    });
});

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 53 (14 by maintainers)

Commits related to this issue

Most upvoted comments

Saw this trick in another issue.

gulp.task('default', function () {
  return gulp.src('test/**/*_test.js')
    .pipe(mocha())
    .once('end', function () {
      process.exit();
    });
});

Had same issue. The cause was a mongoose connection that was opened but not properly caused on tests. Adding

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

fixed this for me.

Using gulp-spawn-mocha fixes the problem for me.

@scippio It may not close its connections at all. That’s the case. TJ a while ago discussed why it was good to put process.exit() in mocha CLI. People argued that developers should take care of closing their connections so mocha doesn’t hang and TJ said yes, people should, but a lot of libraries don’t so we use process.exit() to force them to close their connections.

I ended up closing my connections manually and everything works fine but I see the case to use process.exit() if you don’t want to worry with it or if you use one library that behaves bad and doesn’t close its connections.