捕获Gulp-Mocha错误



我可能缺少一些非常明显的东西,但我无法让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));
});

另外,将处理程序放在整个流中也给出了相同的问题:

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

我还尝试使用plumbercombinegulp-batch无济于事,所以我想我忽略了一些琐碎的东西。

GIST:http://gist.github.com/royjacobs/B518EBAC117E95FF1457

您需要忽略'错误',并始终发射'结束'才能使'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));
});

这使得" Gulp测试"总是返回" 0",这对于连续集成是有问题的,但是我认为我们目前别无选择。

在shuhei kagawa的答案上扩展。

发射端将由于被转换为例外的未误差而阻止吞咽出口。

设置一个观看var以跟踪您是否正在通过手表进行测试,然后取决于您是否正在开发或运行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"]);
});

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

最新更新