Reputation: 46569
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.
I'm building a mocha test in coffeescript. Right at the top of the test I have:
require "../assets/js/theObject.coffee"
debugger
ss = new TheObject()
I'd like to stop on that debugger line because the object in theObject.coffee
isn't being loaded. I'm using node-inspector and it works, sorta.
The process that I have is:
mocha --compilers coffee:coffee-script ./test/theObjectTests.coffee --ui bdd -d --debug-brk
theObject.coffee
to be loaded, then put a breakpoint on the correct line 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 discusses setting 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:
"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 "$0"`
I suspect that might be a windows specific issue.
Env: Windows 7, Webstorm, node 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.
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.
Upvotes: 48
Views: 25747
Reputation: 11728
In addition to the other answers you can also
Note: the critical part of this answer is the inspect , other aspects might need to be changed for i.e. javascript, running without npx, etc. I just wrote the version I have tested (node v16.15.0, mocha ts-mocha 10.2.0)
Upvotes: 0
Reputation: 66430
In regards to either Webstorm or PhpStorm, you can add a specific mocha debug configuration:
You'll have to add via the green, you might give it a name.
If the already installed mocha in the project via:
npm install mocha --save
or
yarn add mocha
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
/
Since my project is a typescript one I had to add:
yarn add ts-node
For a pure js project it should not be necessary.
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.
Upvotes: 1
Reputation: 46569
Edit, years later: the shortest path in Node 6+ is: mocha --debug-brk --inspect ./test.js
coupled with the Node Inspector Manager plugin.
Many weeks later, no answers. Here's the quickest path that I found.
node-inspector
node-inspector
-- it will now be listening on 5858--debug-brk
debugger
on 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. Command line:
node-inspector & mocha --compilers coffee:coffee-script/register ./test/appTests.coffee --ui bdd -d -g "should X then Y" --debug-brk
Upvotes: 64
Reputation: 1344
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
Upvotes: 0
Reputation: 15045
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.
myVar.should.equal(expected);
I find this is often all I need. And I just remove the extra assert(s) I used for debugging when I'm done.
Upvotes: 0
Reputation: 33640
If it's a Node application, then using the integrated Node debugger from the command line is the quickest path to stardom:
$ mocha $args -- debug
Upvotes: 3
Reputation: 3497
Heads up on http://s-a.github.io/iron-node/. This is most efficient software to debug anything Node.js related.
$ iron-node ./node_modules/mocha/bin/_mocha
Upvotes: 6
Reputation: 7451
In Webstorm now you can just set up using a mocha
configuration. Worked pretty much out of the box for me:
Node interpreter: /usr/local/bin/node
Working directory: /Users/me/sites/mysite
Mocha Package: /Users/me/sites/mysite/node_modules/mocha
and then
All in directory
Test directory: /Users/me/sites/mysite/test
It also shows you the parameters it runs with so you can probably copy them to another environment if you need to.
Upvotes: 1
Reputation: 3708
With the latest versions of Mocha and node-inspector, this has been working great for me:
$ node-debug ./node_modules/mocha/bin/_mocha
It loads up the local Mocha executable as the debugged process, stopping on the first line for you to set up your breakpoints.
Upvotes: 8
Reputation: 42635
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)
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.
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.
Upvotes: 11
Reputation: 1645
The alternative way using WebStorm node debugger.
In short:
Path to Node App JS File
Detailed instruction with screenshots by Glenn Block.
Upvotes: 5