“整理”之后,

我是新来的节点,并在任何“适当”的环境中发展。 我已经为我目前的项目安装了gulp,以及摩卡和其他一些模块。 这是我的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... }); 

当我运行'吞咽',似乎完成了所有的任务,但挂起。 我必须按Ctrl + C返回到命令提示符。 我如何才能正确完成?

道歉,伙计! 原来,这是在一揽子 – 摩卡常见问题解答 。 去引用:

testing套件不退出

如果你的testing套件没有退出,那么可能是因为你仍然有一个拖延的callback,通常是由一个开放的数据库连接引起的。 您应closures此连接或执行以下操作:

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

如果你没有运行任何东西,接受的解决scheme将为你工作。 但是,如果您需要在gulp-mocha之后运行任务(例如在部署构build之前运行mochatesting),则可以采用以下解决scheme来防止gulp无限期挂起,同时允许任务在gulp-mocha之后运行:

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

这是gulp ,因为gulp从orchestrator inheritance ,在所有任务运行完成或错误之后,它们会发出事件 。

在gulp任务中添加一个return语句。 或者运行callback。

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

升级到摩卡4后,我可以通过传递给摩卡来解决这个问题。

有关更多信息,请参阅https://boneskull.com/mocha-v4-nears-release/#mochawontforceexit

在使用gulp-mocha时,将该选项添加为exit: true如下所示:

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