Gulp:目标是debugging摩卡testing

我有一套运行我的摩卡testing的gulp.js目标,这个testing的function就像是一个吞噬摩卡的魅力一样。 问题:如何debugging我的摩卡testing运行通过吞咽? 我想使用像node-inspector这样的东西在我的src中设置断点并testing文件来查看正在发生的事情。 我已经可以通过直接调用节点来实现这一点了:

node --debug-brk node_modules/gulp/bin/gulp.js test 

但是我更喜欢为我包装这个吞噬目标,例如:

 gulp.task('test-debug', 'Run unit tests in debug mode', function (cb) { // todo? }); 

想法? 我想避免一个bash脚本或其他单独的文件,因为我正在尝试创build一个可重用的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']); }); 

从@BrianGlaz的一些指导,我想出了以下任务。 结束很简单。 再加上pipe道输出到父母的stdout所以我不必手动处理stdout.on

  // Run all unit tests in debug mode gulp.task('test-debug', function () { var spawn = require('child_process').spawn; spawn('node', [ '--debug-brk', path.join(__dirname, 'node_modules/gulp/bin/gulp.js'), 'test' ], { stdio: 'inherit' }); }); 

您可以使用Node的Child Process类从节点应用程序中运行命令行命令。 在你的情况下,我会build议childprocess.spawn() 。 它充当事件发射器,因此您可以订阅data以从stdout检索输出。 就从内部使用这个方面来说,可能需要做一些工作来返回一个可以传送给另一个吞咽任务的stream。