我有一套gulp.js目标,用于运行我的摩卡测试,其工作方式类似于通过gulp-mocha运行的魅力。问题:我如何调试通过吞咽运行的摩卡测试?我想用node-inspector之类的东西在我的src中设置断点并测试文件以查看发生了什么。我已经能够通过直接调用节点来实现:Gulp:调试摩卡测试的目标
node --debug-brk node_modules/gulp/bin/gulp.js test
但我更喜欢一饮而尽的目标,它包装这对我来说,例如:
gulp.task('test-debug', 'Run unit tests in debug mode', function (cb) {
// todo?
});
想法?我想避免使用bash
脚本或其他单独的文件,因为我试图创建一个可重复使用的gulpfile
,其目标可由不知道吞咽的人使用。
这是我目前gulpfile.js
// gulpfile.js
var gulp = require('gulp'),
mocha = require('gulp-mocha'),
gutil = require('gulp-util'),
help = require('gulp-help');
help(gulp); // add help messages to targets
var exitCode = 0;
// kill process on failure
process.on('exit', function() {
process.nextTick(function() {
var msg = "gulp '" + gulp.seq + "' failed";
console.log(gutil.colors.red(msg));
process.exit(exitCode);
});
});
function testErrorHandler(err) {
gutil.beep();
gutil.log(err.message);
exitCode = 1;
}
gulp.task('test', 'Run unit tests and exit on failure', function() {
return gulp.src('./lib/*/test/**/*.js')
.pipe(mocha({
reporter: 'dot'
}))
.on('error', function (err) {
testErrorHandler(err);
process.emit('exit');
});
});
gulp.task('test-watch', 'Run unit tests', function (cb) {
return gulp.src('./lib/*/test/**/*.js')
.pipe(mocha({
reporter: 'min',
G: true
}))
.on('error', testErrorHandler);
});
gulp.task('watch', 'Watch files and run tests on change', function() {
gulp.watch('./lib/**/*.js', ['test-watch']);
});
也许使用childprocess.exec? http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback –
@BrianGlaz这是一个好主意。唯一的缺点是,在任务完成之前,您不会从流程中获得输出结果,而不是随时待命。有没有办法做到这一点,同时获得逐步输出到标准输出? –
检出child_process.spawn()http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options。它非常相似,但作为一个事件发射器让你附加回调。检查链接的例子。 –