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.
Mocha does not run individual tests in parallel. That means if you hand Mocha a single, lonely test file, it will spawn a single worker process, and that worker process will run the file. If you only have one test file, you'll be penalized for using parallel mode. Don't do that.
A pending test in many test framework is test that the runner decided to not run. Sometime it's because the test is flagged to be skipped. Sometime because the test is a just a placeholder for a TODO. For Mocha, the documentation says that a pending test is a test without any callback.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With