During the twelve days of ZUMO, I posted a couple of articles that showed techniques for unit testing your Mobile Service scripts:
And whilst this is awesome, sometimes you really want to be able to debug a script and go past console.log debugging. If you follow this approach to unit testing your scripts then you can use a couple of techniques to also debug your unit tests (and therefore Mobile Service scripts). I recently purchased a copy of WebStorm during their Mayan Calender end of world promotion (bargain) and it’s a nice tool with built-in node debugging. I asked my team mate Glenn Block if he knew how to use WebStorm to debug Mocha tests. Sure enough, he went off, did the research and posted a great article showing how: Debugging mocha unit tests with WebStorm step by step – follow these steps if you own WebStorm.
For those that don’t have a copy of WebStorm, you can still debug your tests using nothing but node, npm and your favorite WebKit browser (such as Google Chrome).
The first thing you’ll need (assuming you already installed node and npm and the mocha framework) is node-inspector. To install node-inspector, just run this command in npm
npm install -g node-inspector
On my mac, I had to run
sudo npm install -g node-inspector
and enter my password (and note, this can take a while to build the inspector). Next, when you run your unit tests add the --debug-brk switch:
mocha test.js -u tdd --debug-brk
This will start your test and break on the first line. Now you need to start node-inspector in a separate terminal/cmd window:
And you’re ready to start debugging. Just point your webkit browser to the URL shown in the node-inspector window, typically:
Now, unfortunately the first line of code that node and the inspector will break on will be mocha code and it’s all a little big confusing here for a few minutes, but bear with it, because once you’re up and running it gets easier.
The first thing you’ll need to do is advance the script past the line , Mocha = require(‘../’)which will load all the necessary mocha files. Now you can navigate to the file Runnable.js using the left pane.
And in this file, put a break on the first line inside Runnable.prototype.run function:
If you now hit the start/stop button (F8) to run to that breakpoint, the process will have loaded your test files so you can start to add breaks:
Here, I’ve found my test file test.js:
And we’re away. After this, the webkit browser will typically remember your breakpoints so you only have to do this once. So there you go, debugging mocha with node-inspector. Or you could just buy WebStorm.
26 Jan 2013
» Next Post:
Working with Making Waves and VGTV – Mobile Services
« Previous Post:
Dispatching to different query functions in Mobile Services
Comments are closed for this post.
01 Mar 2013
"Or you could just buy WebStorm." - Not to take away from a good instructional post, but I just checked pricing on Webstorm and it seems like a no-brainer! (Esp. given the quality of ReSharper).
22 Mar 2013
Note. The latest version of node does not allow you to use node-inspector (on Windows 8 anyhow). I went to ver 8.16 and it worked fine!