javascript - Gulp:调试 mocha 测试的目标

标签 javascript node.js gulp

我有一组gulp.js运行我的 mocha 测试的目标,这些测试就像运行 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 目标,这些目标可供不了解 gulp 的人使用。

这是我当前的 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' });
  });

关于javascript - Gulp:调试 mocha 测试的目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23612087/

相关文章:

javascript - gulp typescript 时出现 TypeError

javascript - D3 JSON数据转换

javascript - 为什么 React.js 在使用标记库时不加载 HTML 代码

node.js - 在node.js中解密使用A128CBC-HS256加密的JWT

Javascript 数学 Node 红色热敏电阻函数

javascript - 获取 Gulp.js 中目录中的文件数量?

javascript - 为什么这个 jquery 在 IE8 中不通过 AJAX 加载内容?

javascript - 无效 Prop : type check failed for prop

node.js - React 应用程序使用什么服务器端代码

css - 我怎样才能抓取一堆 scss 文件并为所有应用程序制作一个 css 文件