Gulp:目标是调试mocha测试

时间:2022-11-26 00:26:49

I have a set of gulp.js targets for running my mocha tests that work like a charm running through gulp-mocha. Question: how do I debug my mocha tests running through gulp? 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. I am already able to accomplish this by calling node directly:

我喝了一大口。js的目标是运行我的摩卡测试,工作起来就像一个魅力贯穿在古普邦的摩卡。问:我如何调试在gulp中运行的mocha测试?我想使用node-inspector之类的东西在src和测试文件中设置断点,看看发生了什么。我已经可以通过直接调用node来实现这一点:

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

But I'd prefer a gulp target that wraps this for me, e.g.:

但是我更喜欢一个能帮我包装的目标,例如:

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,这些目标对于不了解gulp的人来说是可用的。

Here is my current gulpfile.js

这是我目前的gulpfiley .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']);
});

2 个解决方案

#1


13  

With some guidance from @BrianGlaz I came up with the following task. 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:

在@BrianGlaz的指导下,我完成了以下任务。结果很简单。加上它将所有的输出传输到父节点的stdout,所以我不需要处理stdout。手动:

  // 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' });
  });

#2


4  

You can use Node's Child Process class to run command line commands from within a node app. In your case I would recommend childprocess.spawn(). It acts as an event emitter so you can subscribe to data to retrieve output from 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.

可以使用Node的子进程类在节点应用程序中运行命令行命令。它作为一个事件发送器,因此您可以订阅数据以从stdout检索输出。对于在gulp中使用这个,可能需要做一些工作来返回可以通过管道传输到另一个gulp任务的流。

#1


13  

With some guidance from @BrianGlaz I came up with the following task. 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:

在@BrianGlaz的指导下,我完成了以下任务。结果很简单。加上它将所有的输出传输到父节点的stdout,所以我不需要处理stdout。手动:

  // 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' });
  });

#2


4  

You can use Node's Child Process class to run command line commands from within a node app. In your case I would recommend childprocess.spawn(). It acts as an event emitter so you can subscribe to data to retrieve output from 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.

可以使用Node的子进程类在节点应用程序中运行命令行命令。它作为一个事件发送器,因此您可以订阅数据以从stdout检索输出。对于在gulp中使用这个,可能需要做一些工作来返回可以通过管道传输到另一个gulp任务的流。