【发布时间】:2014-05-12 14:43:24
【问题描述】:
我有一组 gulp.js 目标用于运行我的 mocha 测试,它们的运行就像通过 gulp-mocha 的魅力一样。问题:如何调试通过 gulp 运行的 mocha 测试?我想使用 node-inspector 之类的东西在我的 src 中设置断点并测试文件以查看发生了什么。我已经可以通过直接调用 node 来完成此操作:
node --debug-brk node_modules/gulp/bin/gulp.js test
但我更喜欢为我包装这个的 gulp 目标,例如:
gulp.task('test-debug', 'Run unit tests in debug mode', function (cb) {
// todo?
});
想法?我想避免使用bash 脚本或其他一些单独的文件,因为我正在尝试创建一个可重用的gulpfile,其目标可供不了解 gulp 的人使用。
这是我目前的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? nodejs.org/api/…
-
@BrianGlaz 这是个好主意。唯一的缺点是,在任务完成之前,您不会从流程中获得输出,而不是随用随走。有没有办法在将渐进式输出到标准输出的同时做到这一点?
-
查看 child_process.spawn() nodejs.org/api/…。它非常相似,但充当事件发射器,让您附加回调。检查链接以获取示例。
-
很好,我认为这会很好。现在只需从中创建一个答案,您就会得到一些代表;)
标签: javascript node.js gulp