简体   繁体   中英

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 :

在此输入图像描述

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

I frequently run into this, and I don't know if there's a better solution (if there is I'll be glad to hear it), but I find I have to let the debugger advance to a point where it becomes aware of the additional files I want to debug. Without seeing more of your code I can't give a more specific suggestion about where to advance to, but try to figure out where the test files will be loaded in the source files that are available and advance to there. It'll gradually add more files to the Sources panel as code runs.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM