【问题标题】:node.js mocha fallback to debugger on uncaught errornode.js mocha 在未捕获错误时回退到调试器
【发布时间】:2015-07-10 04:19:53
【问题描述】:

node.js 中有没有办法(最好也是 mocha)来模拟 python 的行为

python -m pdb setup.py test

如果程序抛出未捕获的异常,这将回退到调试接口。

编辑

@robertklep 建议使用未记录的breakOnException 命令,该命令看起来非常棒,但在实践中并没有那么多:

test.js

var hello = {};
function testFunc () {
  anotherFunct();
}

function anotherFunct () {
  var b = hello.unfound;
}
testFunc();

然后它冻结在:

$ node debug test.js
< Debugger listening on port 5858
connecting to port 5858... ok
break in test.js:1
> 1 var hello = {};
  2 function testFunc () {
  3   anotherFunct();
debug> breakOnException
debug> c
debug> backtrace

EDIT2:test.js 首先没有产生错误。以上工作正常。

【问题讨论】:

  • 您可能应该使用像node-inspector 这样的“适当的”远程调试器,REPL 客户端相当有限。 FWIW,您展示的示例没有任何问题,因此代码运行并完成。也许这就是回溯导致它冻结的原因(这显然仍然很糟糕)。
  • 你说得对,我很粗心。谢谢。

标签: javascript node.js debugging mocha.js


【解决方案1】:

将此添加到您的脚本中:

process.on('uncaughtException', function() {
  debugger;
});

并以node debug script.js 开头。这将使您进入 debugger 未捕获的异常/错误。

要让这个(在某种程度上)适用于 Mocha 测试,您可以添加上面的行并按照说明进行操作 here(特别是安装 node-inspector 并使用 --debug-brk 启动 Mocha)。

编辑:Node 调试器还具有中断错误/异常的能力:

$ node debug script.js
debug> breakOnException
debug> c
...

【讨论】:

  • 好吧,调试器调用是在错误上下文中进行的,所以这根本不是很有用......关键是要有一个 repl 来检查发生错误的情况。 Chrome 也有那个不错的“错误中断”按钮(你甚至可以获得令人难以置信的异步堆栈)所以我希望人们会将它移植到节点......
  • @fakedrake 看到我的编辑,Node 也有类似的东西。
  • 哇!这怎么没有记录在案?
猜你喜欢
  • 1970-01-01
  • 2012-02-19
  • 1970-01-01
  • 2018-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-02
  • 2012-12-12
相关资源
最近更新 更多