Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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']);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top