38

有没有办法使用节点检查器通过 Jest 调试单元测试?有时可以逐步了解测试失败的原因

我尝试了几种方法

node-debug jest --runInBand 

从以及首先启动检查员,例如

$ node-inspector
$ node --debug-brk .\node_modules\jest-cli --runInBand

然后导航到http://127.0.0.1:8080/debug?port=5858

我发现偶尔(大约 10 次中的 1 次),调试器会打开 jest src 文件并且可以调试它们。不过,一般来说,调试器中的脚本只包含一个“无域”文件夹和另一个不相关的文件夹。此外,测试脚本本身也不会加载到调试器中。

有没有人试过这个?

4

4 回答 4

16

看起来问题在于jestusing harmonize,它会产生一个子进程以确保使用该--harmony选项。

协调/协调.js,第 30-35 行

var node = child_process.spawn(process.argv[0], ['--harmony'].concat(process.argv.slice(1)), {});
node.stdout.pipe(process.stdout);
node.stderr.pipe(process.stderr);
node.on("close", function(code) {
    process.exit(code);
});

通过注释掉 jest 用来生成协调过程的代码,我能够成功调试 jest 测试(尽管使用 JSX 转换的测试非常慢)。

node_modules/jest-cli/bin/jest.js,文件的最后几行:

if (require.main === module) {
  //harmonize();                  <--- comment out
  _main(function (success) {
    process.exit(success ? 0 : 1);
  });
}

然后你可以运行:

$ node-debug --nodejs --harmony ./node_modules/jest-cli/bin/jest.js --runInBand

Jest 依赖于--harmony存在的标志,所以这就是为什么我们需要用--nodejs --harmony. 我们还添加--runInBand以便测试按顺序运行,而不是并行运行。

这将打开 Web 调试器,您可以调试测试,尽管到达您想要的测试可能会很慢。如果有人知道如何加快速度,请发表评论,我会更新我的答案。

您可以将其添加到您的package.json以使其更容易开始:

...
    scripts: {
        "test": "jest",
        "test-debug": "node-debug --nodejs --harmony ./node_modules/jest-cli/bin/jest.js --runInBand"
    }
...

当然,这个解决方案的主要关注点是jest源代码的编辑。会考虑如何提出拉取请求来使这个坚持下去。

在此处创建 Github 问题https ://github.com/facebook/jest/issues/152

于 2014-10-16T23:15:50.840 回答
13

现在Node >= 6.3 正式支持这一点。
引用Jest 文档

在您的任何测试中放置一条debugger;语句,然后在您的项目目录中运行:

node --debug-brk --inspect ./node_modules/.bin/jest -i [any other arguments here]

这将输出一个可以在 Chrome 中打开的链接。打开该链接后,将显示 Chrome 开发人员工具,并在 Jest CLI 脚本的第一行设置断点(这样做只是为了让您有时间打开开发人员工具并防止 Jest 在您之前执行有时间这样做)。单击屏幕右上角看起来像“播放”按钮的按钮以继续执行。当 Jest 执行包含该debugger语句的测试时,执行将暂停,您可以检查当前范围和调用堆栈。

注意:-icli 选项确保 Jest 在同一进程中运行测试,而不是为单个测试生成进程。通常,Jest 将跨进程的测试运行并行化,但很难同时调试多个进程。

可以在此处找到有关 V8 检查器的更多信息:https ://nodejs.org/api/debugger.html#debugger_v8_inspector_integration_for_node_js

于 2016-09-22T14:32:01.817 回答
5

使用 Node 7.4.0、Jest 18.x 和jest-environment-node-debug包(来自此评论),现在可以使用 chrome devtools 来调试 Jest 测试:

$ npm install -D jest-environment-node-debug
$ node --inspect-brk ./node_modules/.bin/jest -i --env jest-environment-node-debug
于 2017-01-29T02:07:39.107 回答
4

这是一个 Gruntfile.js 配置,用于使用 Grunt 自动化 @Sean 的回答。

grunt testd

或者

grunt testd --tests=MyTestName

或者

grunt testd --tests=MyTestName,AnotherTestName

需要“node-inspector”(必须全局安装才能在您的路径中获取 node-debug bin)、“lodash”、“jest-cli”和“grunt-shell”节点模块。

var _ = require('lodash');

var commaSplitToRegex = function(input) {
  return _.map(input.split(','), function(part) {
    return '(' + part + ')';
  }).join('|');
};

var getTestRegex = function(tests) {
  if (tests) {
    return '.*' + commaSplitToRegex(tests) + '.*';
  }

  return '.*';
}

module.exports = function(grunt) {
  grunt.loadNpmTasks('grunt-shell');

  grunt.initConfig({
    shell: {
      jestd: {
        command: function() {
          var testsRegex = getTestRegex(grunt.option('tests'));
          var cmd = 'node-debug --nodejs --harmony ./node_modules/jest-cli/bin/jest.js --runInBand --config="test_utils/jest.json"';

          if (testsRegex) {
            cmd += ' "' + testsRegex + '"';
          }

          return cmd;
        }
      },
      monkeypatchjest: {
        command: 'sed -i.bak s\\/harmonize\\(\\)\\;\\/\\\\/\\\\/wtf\\/g ./node_modules/jest-cli/bin/jest.js'
      },
      unmonkeypatchjest: {
        command: 'sed -i.bak s\\/\\\\/\\\\/wtf\\/harmonize\\(\\)\\;\\/g ./node_modules/jest-cli/bin/jest.js'
      }
    }
  });

  grunt.registerTask('testd', 'Run tests with debugger.', ['shell:monkeypatchjest', 'shell:jestd']);
};
于 2014-11-06T00:11:28.080 回答