gpt4 book ai didi

javascript - node.js mocha 在未捕获错误时回退到调试器

转载 作者:行者123 更新时间:2023-11-30 00:29:57 25 4
gpt4 key购买 nike

node.js 中有没有一种方法(理想情况下也是 mocha)来模拟 python 的行为

python -m pdb setup.py test

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

编辑

@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 一开始没有产生错误。以上工作正常。

最佳答案

将此添加到您的脚本中:

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
...

关于javascript - node.js mocha 在未捕获错误时回退到调试器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29968154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com