![](/img/trans.png)
[英]Cannot use require in my mocha tests with karma runner via gulp
[英]Gulp: target to debug mocha tests
我有一组gulp.js目标用于运行我的摩卡测试,其工作方式类似于通过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
,其目标可供不知道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的一些指导下,我想出了以下任务。 结束相当简单。 另外,它将所有输出管道传输到父级的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
类从节点应用程序中运行命令行命令。 在你的情况下,我会建议childprocess.spawn() 。 它充当事件发射器,因此您可以订阅data
以从stdout
检索输出。 就在gulp中使用它而言,可能需要做一些工作来返回可以通过管道传输到另一个gulp任务的流。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.