如何使用Node-Inspector调试NodeUnit

11

我可以做以下事情:

  • 我可以使用nodeunit来测试node.js模块。
  • 我可以使用node inspector来调试我的node.js express站点。

但是如何使用node inspector来调试nodeunit测试呢?

我尝试过,但不起作用:

  • nodeunit --debug myNodeUnitModule_test.js,它不起作用。
  • 我尝试安装nodebug,并像这样使用它:nodebug /usr/local/bin/nodeunit myNodeunit_test.js,但在ubuntu上不起作用(No such file or directory),在mac上也不起作用(env: node\r: No such file or directory)。

几乎可以工作 node --debug /usr/local/bin/nodeunit ./routes/edit/bodyTelInfoArraysToObject_test.js

其中/usr/local/bin/nodeunit是命令which nodeunit获得的路径。

在那里得到输出:debugger listening on port 5858,并在那里执行了测试。

但我无法进入调试模式:当我在chrome中打开localhost:8080观看调试时:

  1. 第一次加载时,我看到空的文件列表
  2. 第二次加载:页面未找到。

在我的nodeunit测试中,我写了debugger以停止在那里进行调试。但是没有反应。

2个回答

11

在你的测试中插入 debugger; 命令

exports['Main test'] = function(test){
    debugger;

    test.expect(1);
    test.ok(true, 'Must be ok');
    test.done();
};

开始所有这些

$ node --debug-brk `which nodeunit` test.js

在浏览器中按 F8,然后按 F10,在第一个 debugger; 命令之后你就可以进入下一行了。

但我更喜欢使用 node-supervisor 来启动一切,当测试完成或项目目录中的文件发生变化时,它会自动重新启动测试:

$ npm -g install supervisor node-inspector

$ # console 1
$ # supervisor restarts node-inspector when it quits
$ # ignores file changes
$ supervisor -i . -x node-inspector .

$ # console 2
$ supervisor --debug-brk -- `which nodeunit` test/index.js

3
解决方案如下:
  • 在控制台中输入以下命令:node --debug-brk `which nodeunit` ./path/To/My/NodeUnitTests/nodeunit_test.coffee (注意:`which nodeunit` 需要用反引号包裹)

  • 在另一个控制台中输入:node-inspector &

  • 然后在 Google Chrome 中打开:http://0.0.0.0:8080/debug?port=5858,在这里可以看到 nodeunit 从一开始就进行了调试。在浏览器中点击“继续执行”几次,直到跳转到带有 debugger; 字符串的 nodeunit 测试位置。这样就可以使用 nodeinspector 调试 nodeunit 测试。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接