node.js 使用 mocha 的 --debug-brk 开关启用节点调试器的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14352608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the right way to enable the node debugger with mocha's --debug-brk switch?
提问by Howard Dierking
I have some debugger statements in my module under test and want to run mocha with --debug-brk set and hit my breakpoint so that I can inspect the state of my module. Unfortunately, whenever I run mocha with this option, I end up with a blank cursor on the next line. I can enter text, but there's nothing that appears to be processing my commands (it certainly doesn't look like the node debugger):
我在我的被测模块中有一些调试器语句,我想用 --debug-brk set 运行 mocha 并点击我的断点,以便我可以检查我的模块的状态。不幸的是,每当我使用此选项运行 mocha 时,下一行都会出现一个空白光标。我可以输入文本,但似乎没有任何东西在处理我的命令(它肯定不像节点调试器):
$ mocha --debug-brk tests.js -R spec
debugger listening on port 5858
[BLANK CURSOR]
Am I doing something wrong with how I'm launching mocha?
我在启动摩卡咖啡时做错了什么吗?
采纳答案by ELLIOTTCABLE
To answer the original question, even though I also suggest you look into node-inspector: you can use the CLI debugger built into node through mocha with the debugoption, instead of the --debugor --debug-brkflags. (Notice the lack of dashes.)
要回答最初的问题,即使我还建议您查看node-inspector:您可以使用带有debug选项的mocha 内置到 node 中的 CLI 调试器,而不是--debugor--debug-brk标志。(注意缺少破折号。)
In your example, instead, it would be:
在您的示例中,它将是:
$ mocha debug tests.js -R spec
debugger listening on port 5858
connecting... ok
break in node_modules/mocha/bin/_mocha:7
5 */
6
7 var program = require('commander')
8 , sprintf = require('util').format
9 , path = require('path')
debug> [CURSOR]
Again, debugas above in bold, with no dashes. (=
再次,debug如上以粗体显示,没有破折号。(=
回答by stropitek
Using a recent version of nodejs (>=v6.3.0) and mocha (>=3.1.0), you can use V8 inspector integration.
使用最新版本的 nodejs ( >=v6.3.0) 和 mocha ( >=3.1.0),您可以使用V8 检查器集成。
V8 Inspector integration allows attaching Chrome DevTools to Node.js instances for debugging and profiling
V8 Inspector 集成允许将 Chrome DevTools 附加到 Node.js 实例以进行调试和分析
Usage
用法
--inspectactivates V8 inspector integration, and --debug-brkadds a breakpoint at the beginning. Since nodejs v7.6.0and mocha v3.3.0, you can use the --inspect-brkshorthand for --inspect --debug-brk
--inspect激活 V8 检查器集成,并--debug-brk在开头添加断点。由于nodejs v7.6.0和mocha v3.3.0,您可以使用--inspect-brk简写--inspect --debug-brk
$ mocha --debug-brk --inspect
Debugger listening on port 9229.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
chrome-devtools://devtools/remote/serve_file/@62cd277117e6f8ec53e31b1be58290a6f7ab42ef/inspector.html?experiments=true&v8only=true&ws=localhost:9229/node
With npm scripts
使用 npm 脚本
If you have a npm test script that uses mocha, you can pass the options from npm to your mocha script by using the end of option delimiter--:
如果您有一个使用 mocha 的 npm 测试脚本,您可以使用选项分隔符的结尾将选项从 npm 传递到您的 mocha 脚本--:
$ npm test -- --inspect --debug-brk
$ npm test -- --inspect --debug-brk
Chrome tip
铬尖
Instead of copy-pasting the url each time, go to chrome://inspectand click the appropriate link in the "Remote target" section.
不要每次都复制粘贴网址,而是转到chrome://inspect并单击“远程目标”部分中的相应链接。
回答by Matthew Shanley
I was able to get this to work using node-inspector. I run my test like you show in one shell:
我能够使用node-inspector 让它工作。我像你在一个 shell 中展示的那样运行我的测试:
mocha --debug-brk mocha/test.js
and then run node-inspector in a second shell:
然后在第二个 shell 中运行 node-inspector:
node-inspector
Bringing up the URL that node-inspector spits out in a browser allows me to debug with the web inspector.
调出 node-inspector 在浏览器中输出的 URL 允许我使用 web 检查器进行调试。
http://127.0.0.1:8080/debug?port=5858
回答by Trideep Gogoi
If you have node-insector installed you can debug you Mocha tests without actually running node-inspector first. The easiest way is to
如果您安装了 node-inspector,您可以调试 Mocha 测试,而无需先实际运行 node-inspector。最简单的方法是
node-debug _mocha
That should start debugging the node tests in chrome (also works on Safari)
这应该开始在 chrome 中调试节点测试(也适用于 Safari)
One reason I have seen that the tests do not work is sometimes you gave to try http://localhost:8080/debug?port=5858or http://127.0.0.1:8080/debug?port=5858
我看到测试不起作用的原因之一是有时您尝试尝试http://localhost:8080/debug?port=5858或http://127.0.0.1:8080/debug?port=5858
回答by Tom Berghuis
run mocha with flag --inspect-brkand click 'open dedicated DevTools for node' in chrome from page chrome://inspect. In dedicated DevTools window add connection localhost:9229to connect automatically.
运行带有标志的 mocha,--inspect-brk然后在页面 chrome 中单击“为节点打开专用 DevTools” chrome://inspect。在专用的 DevTools 窗口中添加连接localhost:9229以自动连接。
Also add a debuggerstatement to the file you want debug.
还要debugger向要调试的文件添加一条语句。
(this is using latest versions of node and chrome as of October 2017)
(这是使用截至 2017 年 10 月的最新版本的 node 和 chrome)

