node.js 调试 mocha 测试的阻力最小的路径是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14285201/
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 least resistance path to debugging mocha tests?
提问by jcollum
Edit Nov 2016: Node now has a built in debugger that you can start with --inspect. This answer explains it: https://stackoverflow.com/a/39901169/30946.
2016 年 11 月编辑:Node 现在有一个内置的调试器,您可以从--inspect. 这个答案解释了它:https: //stackoverflow.com/a/39901169/30946。
I'm building a mocha test in coffeescript. Right at the top of the test I have:
我正在用 coffeescript 构建一个 mocha 测试。在测试的顶部,我有:
require "../assets/js/theObject.coffee"
debugger
ss = new TheObject()
I'd like to stop on that debugger line because the object in theObject.coffeeisn't being loaded. I'm using node-inspector and it works, sorta.
我想在调试器行上停下来,因为theObject.coffee没有加载in 中的对象。我正在使用节点检查器,它可以工作,有点。
The process that I have is:
我的流程是:
- start node-inspector
- run the test at the command line with
mocha --compilers coffee:coffee-script ./test/theObjectTests.coffee --ui bdd -d --debug-brk - go to the node-inspector page, refresh it if it is already open
- wait for the file
theObject.coffeeto be loaded, then put a breakpoint on the correct line
- 启动节点检查器
- 在命令行运行测试
mocha --compilers coffee:coffee-script ./test/theObjectTests.coffee --ui bdd -d --debug-brk - 转到节点检查器页面,如果它已经打开则刷新它
- 等待文件
theObject.coffee被加载,然后在正确的行上放置一个断点
There must be an easier way. It seems like I should be able to have a debugger running and just have it stop on that debugger line, but I'm not able to find that.
必须有更简单的方法。似乎我应该能够运行调试器并让它停在调试器行上,但我找不到。
I have WebStorm, which has a debugger (this article discussessetting it up to run mocha tests, but it didn't help me), but when I start it, it fails. The command that's running in the WebStorm debug window is:
我有 WebStorm,它有一个调试器(本文讨论了设置它以运行 mocha 测试,但它没有帮助我),但是当我启动它时,它失败了。在 WebStorm 调试窗口中运行的命令是:
"C:\Program Files\nodejs\node.exe" --debug-brk=64232 C:\Users\jcollum\AppData\Roaming\npm\_mocha
C:\Users\jcollum\AppData\Roaming\npm\_mocha:2
basedir=`dirname "$ node-debug ./node_modules/mocha/bin/_mocha
"`
I suspect that might be a windows specific issue.
我怀疑这可能是 Windows 特定的问题。
Env: Windows 7, Webstorm, node 0.8.16, mocha 1.7.4, git-bash
环境:Windows 7、Webstorm、节点 0.8.16、mocha 1.7.4、git-bash
The question: if you're starting from scratch with Mocha, what's the easiest way to get a debugger going that will stop on a debugger line easily? Easy is the keyword here.
问题是:如果您从头开始使用 Mocha,那么让调试器运行并轻松停在调试器线上的最简单方法是什么?Easy 是这里的关键词。
Edit: since asking this I've stopped using Windows and am working in Ubuntu. My mocha debugging process (which I use infrequently) is the same.
编辑:自从问这个我已经停止使用 Windows 并在 Ubuntu 中工作。我的 mocha 调试过程(我很少使用)是一样的。
回答by jcollum
Edit, years later: the shortest path in Node 6+ is: mocha --debug-brk --inspect ./test.jscoupled with the Node Inspector Manager plugin.
多年后编辑:Node 6+ 中的最短路径是:mocha --debug-brk --inspect ./test.js与 Node Inspector Manager 插件相结合。
Many weeks later, no answers. Here's the quickest path that I found.
几个星期后,没有答案。这是我找到的最快路径。
- write mocha tests
- install
node-inspector - start
node-inspector-- it will now be listening on 5858 - start the mocha test with
--debug-brk - at this point the mocha test is paused on the first line
- open a web browser and go to localhost:5858
- (optional: add a debugger line at the top of your test file, set breakpoints after it stops in that file)
- hit F10 to get the code to go
- node-inspector will stop on any line that has
debuggeron it. Occasionally it won't move the code file's window to the right place, so you'll have to hit F10 to get it to step to the next line and show where it's at in the file.
- 编写 mocha 测试
- 安装
node-inspector - 开始
node-inspector——它现在将在 5858 上监听 - 开始摩卡测试
--debug-brk - 此时摩卡测试在第一行暂停
- 打开 Web 浏览器并转到 localhost:5858
- (可选:在测试文件的顶部添加调试器行,在该文件中停止后设置断点)
- 按 F10 以获取代码
- node-inspector 将停在任何有
debugger它的行上。有时它不会将代码文件的窗口移动到正确的位置,因此您必须按 F10 才能让它跳到下一行并显示它在文件中的位置。
Command line:
命令行:
node-inspector & mocha --compilers coffee:coffee-script/register ./test/appTests.coffee --ui bdd -d -g "should X then Y" --debug-brk
node-inspector & mocha --compilers coffee:coffee-script/register ./test/appTests.coffee --ui bdd -d -g "should X then Y" --debug-brk
回答by captainclam
In addition to @jcollum's answer above, I have found instead of using the --debug-brk flag, it is better to just use the --debug flag with -w (watch)
除了上面@jcollum 的回答之外,我发现不要使用 --debug-brk 标志,最好只使用带有 -w 的 --debug 标志(观察)
That way, when you add and remove debugger lines from your code, mocha will reload the tests automatically and your node-inspector will pause on the appropriate line.
这样,当您从代码中添加和删除调试器行时,mocha 将自动重新加载测试,并且您的节点检查器将在适当的行上暂停。
This saves having to revisit the terminal constantly restarting the tests, then needlessly hitting "continue" in the debugger to get past the first line of the source.
这样就不必重新访问终端,不断重新启动测试,然后在调试器中不必要地点击“继续”以通过源代码的第一行。
回答by doingweb
With the latest versions of Mocha and node-inspector, this has been working great for me:
使用最新版本的 Mocha 和node-inspector,这对我来说非常有用:
$ mocha $args -- debug
It loads up the local Mocha executable as the debugged process, stopping on the first line for you to set up your breakpoints.
它加载本地 Mocha 可执行文件作为调试进程,在第一行停止以设置断点。
回答by Stephan Ahlf
Heads up on http://s-a.github.io/iron-node/. This is most efficient software to debug anything Node.js related.
注意http://sa.github.io/iron-node/。这是调试任何与 Node.js 相关的最有效的软件。
$ iron-node ./node_modules/mocha/bin/_mocha
$ iron-node ./node_modules/mocha/bin/_mocha
回答by Brock
The alternative way using WebStorm node debugger.
使用 WebStorm 节点调试器的替代方法。
In short:
简而言之:
- You need WebStorm
- Create new Node debug profile in WebStorm
- Set path to your mocha binary into
Path to Node App JS File - Add breakpoints and start the session from WebStorm
- 你需要网络风暴
- 在 WebStorm 中创建新的节点调试配置文件
- 将 mocha 二进制文件的路径设置为
Path to Node App JS File - 添加断点并从 WebStorm 启动会话
Detailed instructionwith screenshots by Glenn Block.
Glenn Block 截图的详细说明。
回答by Filip Dupanovi?
If it's a Node application, then using the integrated Node debugger from the command line is the quickest path to stardom:
如果它是一个 Node 应用程序,那么从命令行使用集成的 Node 调试器是成为明星的最快途径:
Node interpreter: /usr/local/bin/node
Working directory: /Users/me/sites/mysite
Mocha Package: /Users/me/sites/mysite/node_modules/mocha
回答by timhc22
In Webstorm now you can just set up using a mochaconfiguration. Worked pretty much out of the box for me:
现在在 Webstorm 中,您可以使用mocha配置进行设置。对我来说几乎是开箱即用的:
All in directory
Test directory: /Users/me/sites/mysite/test
and then
进而
npm install mocha --save
It also shows you the parameters it runs with so you can probably copy them to another environment if you need to.
它还向您显示了它运行时使用的参数,因此您可以根据需要将它们复制到另一个环境中。
回答by k0pernikus
In regards to either Webstorm or PhpStorm, you can add a specific mocha debug configuration:
对于 Webstorm 或 PhpStorm,您可以添加特定的 mocha 调试配置:


You'll have to add via the green, you might give it a name.
你必须通过绿色添加,你可以给它一个名字。
If the already installed mocha in the project via:
如果项目中已经安装了 mocha 通过:
yarn add mocha
or
或者
yarn add ts-node
it will find the according module in your project.
它将在您的项目中找到相应的模块。
I had to provide the correct path to my unit tests, and hit the mark for Include subdirectories/
我必须为我的单元测试提供正确的路径,并达到Include subdirectories/
Since my project is a typescript one I had to add:
由于我的项目是一个打字稿,我不得不添加:
myVar.should.equal(expected);
For a pure js project it should not be necessary.
对于纯 js 项目,应该没有必要。
Now you can run the entire test suit, and you can then pick single test cases from the list and run them on and debug them on their own.
现在您可以运行整个测试套件,然后您可以从列表中选择单个测试用例并运行它们并自行调试它们。
回答by Cory House
None of the existing answers mention the path of least resistance: When you need to debug Mocha tests, you can simply add another assert that checks the value you'd like to debug.
现有答案都没有提到阻力最小的路径:当您需要调试 Mocha 测试时,您只需添加另一个断言来检查您想要调试的值。
##代码##I find this is often all I need. And I just remove the extra assert(s) I used for debugging when I'm done.
我发现这通常是我所需要的。我只是在完成后删除了用于调试的额外断言。
回答by stropitek
A modern way to do this is to use nodejs's inspector integration feature. It's fairly simple to use. I've already written a detailed explanation of how to use it in this post

