2015-10-17 56 views
10

我是新来的节点并在任何“适当”环境下开发。我已经为我当前的项目安装了gulp,还有mocha和其他一些模块。这里是我的gulpfile.js:“Finishing”之后的吞咽挂钩

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

当我运行“一饮而尽”,这似乎完成所有工作,但挂起。我必须按Ctrl + C返回到命令提示符。我如何才能正确完成?

+0

您自己运行的任何任务('一饮而尽test','一饮而尽lint'),难道他们挂?我已经在这里剪切和粘贴你的代码,并且没有任何问题可以运行它。没有东西挂起。 – Louis

+0

我会稍后再试,谢谢。 – Ooberdan

回答

15

道歉,乡亲们!原来,这是在gulp-mocha FAQ解决。引述:

测试套件不退出

如果您的测试套件没有退出可能是因为你还有一个挥之不去的回调,最经常的开放数据库连接 造成的。您应该关闭此连接或执行以下操作:

gulp.task('default', function() { 
    return gulp.src('test.js') 
     .pipe(mocha()) 
     .once('error', function() { 
      process.exit(1); 
     }) 
     .once('end', function() { 
      process.exit(); 
     }); 
}); 
2

在gulp任务中添加return语句。或者运行回调。

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

我已经尝试了回调,并添加一个返回无效。 – Ooberdan

3

如果没有gulp-mocha后运行任何东西,接受的解决方案会为你工作。但是,如果你需要gulp-mocha后运行任务(例如部署构建之前运行摩卡测试),这里是一个将防止gulp无限期地挂起,同时仍允许任务运行gulp-mocha后一种解决方案:

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

这工作,因为gulpinheritsorchestrator其中emits the events分别在所有任务完成或错误后运行。

2

升级到摩卡4后,我可以通过将--exit传递给摩卡来解决此问题。

请参阅https://boneskull.com/mocha-v4-nears-release/#mochawontforceexit了解更多信息。

当使用一饮而尽,摩卡,添加选项,exit: true为:

gulp.task('test', function() { 
    return gulp.src(['tests/**/*.spec.js'], {read: false}) 
    .pipe(mocha({ exit: true })); 
});