Search code examples
javascriptnode.jsmocha.jsnode-inspector

Debug remote mocha.js test with node-inspector?


I have a test file on a remote machine and I want to walk through it with node-inspector. So, on the remote machine (Vagrantfile):

node-inspector &
mocha --debug-brk foo.test.js

Then, on my dev machine I open Canary and go to:

http://127.0.0.1:8080/debug?ws=127.0.0.1:8080&port=5858

However, I'm not able to debug my test, since the debugger will break at the first line in node_modules/mocha/bin/_mocha, and my test file isn't visible in the Sources tab:

enter image description here

I tried setting a breakpoint inside _mocha, on line 398:

runner = mocha.run(program.exit ? exit : exitLater);

But when I try to 'step into', to see the run function execute, it doesn't step in. I can see output in the console, so it does execute though. If I set a breakpoint directly in the run function, it won't break there.

Also, the test file never appears in the "Sources" tab so I can't set breakpoints in it. I also tried adding a debugger statement to it but it still doesn't break there.

How can I make node-inspector show the test file, and step through it ?

node v0.12.0
node-inspector v0.10.0
mocha v2.2.4

Solution

  • There are actually 2 problems:

    1. breakpoints not respected
    2. test files not visible

    The first problem was fixed in the recently released node-inspector@v0.10.1. So, breakpoints will be respected anywhere.

    There is still the second issue. As @JMM said, the list of files in the 'Sources' tab is dynamic, and test files won't appear there when the process breaks. What I ended up doing is setting a breakpoint just before the test function is run, in mocha/lib/runnable.js#266, on this line:

    var result = fn.call(ctx);
    

    fn is the test function. Once you step into it, the test file will appear in the Sources tab and the debugger's cursor will be on the first line of the test function.