【发布时间】:2016-10-11 21:44:27
【问题描述】:
var jasmine = require('gulp-jasmine');
var debug = require('gulp-debug');
var testFunc = function(platform, testType) {
var timer = startTimer();
console.log('started!');
return gulp.src('./src/**/_test/**/*.mem.js')
.pipe(jasmine({
verbose: !!args.verbose,
timeout: 15000
}))
.pipe(debug())
.on('error', function(e) {
console.log('started!');
throw e;
})
.on('end', function() {
console.log('end!');
console.log(stopTimer(timer));
});
};
gulp.task('mem', function(done) {
testFunc('chrome', 'mem').on('end', function() {
console.log('done!');
done();
});
});
在 gulp v3.9.1 上。上面的代码在没有记录“end!”的情况下完成。或“完成!”到控制台。我该怎么做才能做到这一点?
My console output:
> gulp mem
[13:10:39] Using gulpfile ~/foo/bar/gulpfile.js
[13:10:39] Starting 'mem'...
started!
[13:10:39] gulp-debug: src/components/avatar/_test/avatar.mem.js
[13:10:39] gulp-debug: src/factories/store/_test/store.mem.js
[13:10:39] gulp-debug: 2 items
更新:
采纳了以下 Lentus 的建议,该建议适用于同步任务,例如 gulp-debug。但是异步的,比如 gulp-jasmine,流在任务完成之前就结束了。您还会注意到“X 秒后完成的 'mem'”没有打印出来:
My console output:
> gulp mem
[13:10:39] Using gulpfile ~/foo/bar/gulpfile.js
[13:10:39] Starting 'mem'...
started!
ended!
...
3 specs, 0 failures
Finished in 18.5 seconds
【问题讨论】:
-
在对数据进行任何操作之前设置事件。你试过吗?
-
嗯,这样就成功了...我很好奇为什么我看到的每个 gulp 示例都在管道列表的最后而不是开头添加了事件处理程序?
-
它们可能是运行时间较长的任务的示例。但一般来说,对于您希望尽早设置监听器的事件
-
此外,它似乎只适用于“同步”任务,如调试()。如果我在那里执行一个异步任务(比如 gulp-jasmine,这就是这个进程实际上最终会运行的),“end”甚至会在该任务完成之前触发......
-
哦,是的,异步任务是另一回事。这就是传递给
mem任务的done函数的目的。它使您可以更好地控制事件的顺序。一旦管道完成运行它的代码,就会调用 end,如果里面的代码设置了一个异步任务,则该函数在技术上已经完成,因此它会触发 end 事件
标签: javascript node.js gulp streaming