In my gulp build, I would like to “interrupt” the build process if my tests on the server module fail, but I'm not sure how to do this.
I am currently using the node request module to run some unit tests on the server side:
gulp.task("run-server-tests", function(){ var serverTestUrl = "http://myurl"; // returns test results in json format request(serverTestUrl, function (error, response, body) { var responseData = JSON.parse(body); if(responseData.isSuccess){ console.log(responseData.message); // nice! continue with rest of build (js, css tasks, etc.) } else { open(serverTestUrl + "&render"); // show unit test failures // err.... gulp.abortProcessing(); ???? } }); });
==================================================== =============
** UPDATE
After helpful feedback from Martin and Nick, I divided my collection into the simplest example that I could come up with to try both sentences, which are:
- Use the callback method automatically passed to the task function to abort the assembly, and
- Use promises to abort
Here is my updated gulpfile.js:
var gulp = require("gulp"); var plugins = require('gulp-load-plugins')(); var Q = require("q"); gulp.task("task-1-option-1", function(callback){ plugins.util.log("task 1 running"); callback("unit test failed, stop build"); // let just assume the unit tests fail }); gulp.task("task-1-option-2", function(callback){ plugins.util.log("task 1 running"); var deferred = Q.defer(); setTimeout(function(){ deferred.reject("unit test failed, stop build"); // again, let assume the unit tests fail }, 2000); return deferred.promise; }); gulp.task("task-2-option-1", ["task-1-option-1"], function(){ // if this runs, the build didn't abort as expected plugins.util.log("task 2 running"); }); gulp.task("task-2-option-2", ["task-1-option-2"], function(){ // if this runs, the build didn't abort as expected plugins.util.log("task 2 running"); }); // trigger option-1 (Nick suggestion) gulp.task("option-1", ["task-1-option-1", "task-2-option-1"]); // trigger option-2 (Martin suggestion) gulp.task("option-2", ["task-1-option-2", "task-2-option-2"]);
==================================================== ===============
The problem is that although both of these approaches seem to “interrupt” the build, both of them also call gulp for a “hard error” in the terminal (cygwin). I am wondering if this is because I run these tests on windows, because, from my limited experience, all node / gulp seems to break for me on windows.
This is the result that I get for each task. First, the callback option:
$ gulp option-1 [10:45:32] Using gulpfile C:\users\bfitzgerald\desktop\gulp-test\gulpfile.js [10:45:32] Starting 'task-1-option-1'... [10:45:32] task 1 running [10:45:32] 'task-1-option-1' errored after 79 ms [10:45:32] Error: unit test failed, stop build at formatError (C:\Users\bfitzgerald\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:169:10) at Gulp.<anonymous> (C:\Users\bfitzgerald\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:195:15) at Gulp.emit (events.js:107:17) at Gulp.Orchestrator._emitTaskDone (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\index.js:264:8) at C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\index.js:275:23 at finish (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:21:8) at cb (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:29:3) at Gulp.<anonymous> (C:\users\bfitzgerald\desktop\gulp-test\gulpfile.js:9:2) at module.exports (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:34:7) at Gulp.Orchestrator._runTask (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\index.js:273:3)
And here is the output for the promises option:
$ gulp option-2 [10:46:50] Using gulpfile C:\users\bfitzgerald\desktop\gulp-test\gulpfile.js [10:46:50] Starting 'task-1-option-2'... [10:46:50] task 1 running [10:46:52] 'task-1-option-2' errored after 2.08 s [10:46:52] Error: unit test failed, stop build at formatError (C:\Users\bfitzgerald\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:169:10) at Gulp.<anonymous> (C:\Users\bfitzgerald\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:195:15) at Gulp.emit (events.js:107:17) at Gulp.Orchestrator._emitTaskDone (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\index.js:264:8) at C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\index.js:275:23 at finish (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:21:8) at C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:45:4 at _rejected (C:\users\bfitzgerald\desktop\gulp-test\node_modules\q\q.js:804:24) at C:\users\bfitzgerald\desktop\gulp-test\node_modules\q\q.js:830:30 at Promise.when (C:\users\bfitzgerald\desktop\gulp-test\node_modules\q\q.js:1064:31)
Therefore, the sentences seem correct in the sense that they stop the second task, but something still seems wrong, since gulp is launching some kind of hard error. Can anyone shed some light on this for me?