Reputation: 730
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?
Upvotes: 11
Views: 10392
Reputation: 103
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 }));
});
Upvotes: 3
Reputation: 13140
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.
Upvotes: 4
Reputation: 730
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(); }); });
Upvotes: 16
Reputation: 477
Add a return statement inside the gulp task. Or run the callback.
gulp.task('default', ['lint','test'], function (next) {
// This will only run if the lint task is successful...
next();
});
Upvotes: 2