繁体   English   中英

捕捉gulp-mocha错误

[英]Catching gulp-mocha errors

我可能会遗漏一些非常明显的东西,但我不能让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无效,所以我想我忽略了一些微不足道的事情。

要点: 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 test'始终返回'0',这对于持续集成来说是个问题,但我认为我们现在别无选择。

扩展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"]);
});

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM