简体   繁体   English

Gulp:调试mocha测试的目标

[英]Gulp: target to debug mocha tests

I have a set of gulp.js targets for running my mocha tests that work like a charm running through gulp-mocha . 我有一组gulp.js目标用于运行我的摩卡测试,其工作方式类似于通过gulp-mocha运行的魅力。 Question: how do I debug my mocha tests running through gulp? 问题:如何调试通过gulp运行的mocha测试? I would like to use something like node-inspector to set break points in my src and test files to see what's going on. 我想使用像node-inspector这样的东西在我的src和测试文件中设置断点,看看发生了什么。 I am already able to accomplish this by calling node directly: 我已经能够通过直接调用node来完成此任务:

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

But I'd prefer a gulp target that wraps this for me, eg: 但是我更喜欢一个为我包装的gulp目标,例如:

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

Ideas? 想法? I want to avoid a bash script or some other separate file since I'm trying to create a reusable gulpfile with targets that are usable by someone who doesn't know gulp. 我想避免使用bash脚本或其他单独的文件,因为我正在尝试创建一个可重用的gulpfile ,其目标可供不知道gulpfile的人使用。

Here is my current gulpfile.js 这是我目前的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']);
});

With some guidance from @BrianGlaz I came up with the following task. 在@BrianGlaz的一些指导下,我想出了以下任务。 Ends up being rather simple. 结束相当简单。 Plus it pipes all output to the parent's stdout so I don't have to handle stdout.on manually: 另外,它将所有输出管道传输到父级的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' });
  });

You can use Node's Child Process class to run command line commands from within a node app. 您可以使用Node的Child Process类从节点应用程序中运行命令行命令。 In your case I would recommend childprocess.spawn() . 在你的情况下,我会建议childprocess.spawn() It acts as an event emitter so you can subscribe to data to retrieve output from stdout . 它充当事件发射器,因此您可以订阅data以从stdout检索输出。 In terms of using this from within gulp, some work would probably need to be done to return a stream that could be piped to another gulp task. 就在gulp中使用它而言,可能需要做一些工作来返回可以通过管道传输到另一个gulp任务的流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM