Chr*_*ery 13 javascript node.js gulp
我有一组gulp.js目标用于运行我的摩卡测试,其工作方式类似于通过gulp-mocha运行的魅力.问题:如何调试通过gulp运行的mocha测试?我想使用像node-inspector这样的东西在我的src和测试文件中设置断点,看看发生了什么.我已经能够通过直接调用节点来完成此任务:
node --debug-brk node_modules/gulp/bin/gulp.js test
Run Code Online (Sandbox Code Playgroud)
但是我更喜欢一个为我包装的gulp目标,例如:
gulp.task('test-debug', 'Run unit tests in debug mode', function (cb) {
// todo?
});
Run Code Online (Sandbox Code Playgroud)
想法?我想避免使用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']);
});
Run Code Online (Sandbox Code Playgroud)
Chr*_*ery 14
在@BrianGlaz的一些指导下,我想出了以下任务.结尾相当简单.另外,它将所有输出都输出到父级,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' });
});
Run Code Online (Sandbox Code Playgroud)