捕捉gulp-mocha错误

Roy*_*obs 39 javascript mocha.js gulp

我可能会遗漏一些非常明显的东西,但我无法gulp-mocha捕捉到错误,导致我的gulp watch任务在每次测试失败时结束.

这是一个非常简单的设置:

gulp.task("watch", ["build"], function () {
  gulp.watch([paths.scripts, paths.tests], ["test"]);
});

gulp.task("test", function() {
  return gulp.src(paths.tests)
    .pipe(mocha({ reporter: "spec" }).on("error", gutil.log));
});
Run Code Online (Sandbox Code Playgroud)

或者,将处理程序放在整个流上也会产生同样的问题:

gulp.task("test", function() {
  return gulp.src(paths.tests)
    .pipe(mocha({ reporter: "spec" }))
    .on("error", gutil.log);
});
Run Code Online (Sandbox Code Playgroud)

我已经使用也试过plumber,combinegulp-batch没有用,所以我想我俯瞰一些小事.

要点:http://gist.github.com/RoyJacobs/b518ebac117e95ff1457

Shu*_*awa 66

您需要忽略'错误'并始终发出'结束'以使'gulp.watch'工作.

function handleError(err) {
  console.log(err.toString());
  this.emit('end');
}

gulp.task("test", function() {
  return gulp.src(paths.tests)
    .pipe(mocha({ reporter: "spec" })
    .on("error", handleError));
});
Run Code Online (Sandbox Code Playgroud)

这使得'gulp test'始终返回'0',这对于持续集成来说是个问题,但我认为我们现在别无选择.


abo*_*ter 41

扩展Shuhei Kagawa的答案..

发送结束将阻止由于未被捕获的错误被转换为异常而导致gulp退出.

设置观察变量以跟踪您是否正在通过监视运行测试,然后退出或不退出,具体取决于您是在开发还是运行CI.

var watching = false;

function onError(err) {
  console.log(err.toString());
  if (watching) {
    this.emit('end');
  } else {
    // if you want to be really specific
    process.exit(1);
  }
}

gulp.task("test", function() {
  return gulp.src(paths.tests)
    .pipe(mocha({ reporter: "spec" }).on("error", onError));
});

gulp.task("watch", ["build"], function () {
  watching = true;
  gulp.watch([paths.tests], ["test"]);
});
Run Code Online (Sandbox Code Playgroud)

然后可以将其用于开发和CI