Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp hangs after "Finishing"

Tags:

node.js

gulp

I'm new to node and developing in any sort of "proper" environment. I've installed gulp for my current project, along with mocha and a few other modules. Here is my gulpfile.js:

var gulp = require('gulp');
var mocha = require('gulp-mocha');
var eslint = require('gulp-eslint');

gulp.task('lint', function () {
    return gulp.src(['js/**/*.js'])
        // eslint() attaches the lint output to the eslint property 
        // of the file object so it can be used by other modules. 
        .pipe(eslint())
        // eslint.format() outputs the lint results to the console. 
        // Alternatively use eslint.formatEach() (see Docs). 
        .pipe(eslint.format())
        // To have the process exit with an error code (1) on 
        // lint error, return the stream and pipe to failOnError last. 
        .pipe(eslint.failOnError());
});

gulp.task('test', function () {
    return gulp.src('tests/test.js', {read: false})
        // gulp-mocha needs filepaths so you can't have any plugins before it 
        .pipe(mocha({reporter: 'list'}));
});

gulp.task('default', ['lint','test'], function () {
    // This will only run if the lint task is successful...
});

When I run 'gulp', it appears to complete all its tasks, but hangs. I have to ctrl+c to get back to the command prompt. How do I get it to finish properly?

like image 987
Ooberdan Avatar asked Oct 17 '15 20:10

Ooberdan


3 Answers

After upgrading to mocha 4, I was able to resolve this by passing --exit to mocha.

See https://boneskull.com/mocha-v4-nears-release/#mochawontforceexit for more information.

When using gulp-mocha, add the option as exit: true as in:

gulp.task('test', function () {
  return gulp.src(['tests/**/*.spec.js'], {read: false})
  .pipe(mocha({ exit: true }));
});
like image 117
Bibble Avatar answered Oct 22 '22 13:10

Bibble


Apologies, folks! Turns out that this is addressed in the gulp-mocha FAQ. To quote:

Test suite not exiting

If your test suite is not exiting it might be because you still have a lingering callback, most often caused by an open database connection. You should close this connection or do the following:

gulp.task('default', function () {
    return gulp.src('test.js')
        .pipe(mocha())
        .once('error', function () {
            process.exit(1);
        })
        .once('end', function () {
            process.exit();
        });
});
like image 39
Ooberdan Avatar answered Oct 22 '22 11:10

Ooberdan


If you are not running anything after gulp-mocha, the accepted solution will work for you. However, if you need to run tasks after gulp-mocha (for example running mocha tests before deploying a build) here's a solution that will prevent gulp from hanging indefinitely while still allowing tasks to run after gulp-mocha:

gulp.on('stop', () => { process.exit(0); });
gulp.on('err', () => { process.exit(1); });

This works because gulp inherits from orchestrator which emits the events after all tasks have run to completion or error, respectively.

like image 3
Joel B Avatar answered Oct 22 '22 12:10

Joel B