【问题标题】:How to run second task only when karma test task is successful with Gulp?仅当 Gulp 的业力测试任务成功时,如何运行第二个任务?
【发布时间】:2014-02-24 23:28:03
【问题描述】:

我正在使用 Gulp。我有一个deploy 任务在test 任务之后运行。问题是即使测试失败,deploy 任务也会运行。有没有办法只在 gulp 中测试成功时运行deploy 任务?

gulp.task('test', function() {
  return gulp.src('some_test_tile')
    .pipe(karma({
      configFile: 'karma.conf.js',
      action: 'run'
    }));
});

gulp.task('deploy', ['test'], function() {
  return gulp.src(paths.scripts)
    .pipe(gulp.dest(paths.dest));
});

我正在使用 gulp-karma 运行 Karma 测试。

【问题讨论】:

  • 你在 karma 上使用什么测试框架?当测试失败时,是否可以让你的框架抛出异常?我相信这会阻止后续的 gulp 任务运行。

标签: gulp gulp-karma


【解决方案1】:

您的任务是否在依赖项完成之前运行?确保您的依赖任务正确使用异步运行提示。

如果其fn 执行以下操作之一,则可以使任务异步:

  • 接受回调
  • 返回一个流
  • 返回一个承诺

查看API documentation上的示例

var gulp = require('gulp');

// takes in a callback so the engine knows when it'll be done
gulp.task('one', function (cb) {
    // do stuff -- async or otherwise
    cb(err); // if err is not null and not undefined, the run will stop, and note that it failed
});

// identifies a dependent task must be complete before this one begins
gulp.task('two', ['one'], function () {
    // task 'one' is done now
});

gulp.task('default', ['one', 'two']);

【讨论】:

    【解决方案2】:

    gulp-karma example 说要在 karma 管道之后添加 .on('error', ...) ,并手动抛出错误以确保 gulp 在任何测试失败时退出非零。应该这样做。

    【讨论】:

      猜你喜欢
      • 2014-12-24
      • 1970-01-01
      • 2013-11-13
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 1970-01-01
      • 2019-06-29
      • 2015-04-09
      相关资源
      最近更新 更多