3

I am using Gulp. I have a deploy task that runs after test task. The problem is that deploy task runs even if test failed. Is there a way to run deploy task only when tests are successful in gulp?

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));
});

I am using gulp-karma to run Karma tests.

Evgenii
  • 36,389
  • 27
  • 134
  • 170
  • What testing framework are you using with karma? Is it possible to make your framework throw an exception when a test fails? I believe that would stop subsequent gulp tasks from running. – Ben Feb 28 '14 at 17:07

2 Answers2

2

Are your tasks running before the dependencies are complete? Make sure your dependency tasks are correctly using the async run hints.

Tasks can be made asynchronous if its fn does one of the following:

  • Accept a callback
  • Return a stream
  • Return a promise

See example on 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']);
Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68
1

The gulp-karma example says to add .on('error', ...) after the pipe to karma, and manually throw the error to ensure gulp exits non-zero if any tests fail. That should do it.

macu
  • 379
  • 1
  • 4
  • 10