In debug mode, mocha doesn't stop on debugger statements in a spec file when using node-inspector

跟風遠走 提交于 2019-12-23 19:39:25

问题


When I run mocha with --debug-brk and open chrome dev tools with node-inspector, the debugger skips over any debugger statements that I put in my specfile.

I can get debugger statements to work in module files provided I follow this trick of placing a breakpoint at the bottom of the mocha lib.

Has anybody else seen this problem?


回答1:


It seems that everybody should be seeing the same problem with node-inspector version 0.2.0beta4.

The problem is in the way how breakpoints are managed:

  • The front-end remembers break-points in browser's local storage and restores them after the relevant file is loaded.
  • When you start mocha with --debug-brk and stop on the first line, your specfiles are not loaded yet, so the front-end does not restore your breakpoints.
  • When you resume mocha execution, front-end can't restore breakpoints quickly enough in the short window between a specfile is parsed and run. In fact mocha may exit before the V8 debugger has a change to notify front-end about new scripts being parsed!

Another workaround for this issue is to add debugger; statement in the specfile where you want to trigger a breakpoint.

EDIT

Note that the solution mentioned in node-inspector issue on github will work if you set breakpoint inside your it callback (i.e. spec implementation) but it won't help you with setting a breakpoint in the code which builds spec description (i.e. top-level code in your specfile and all describe callbacks).

Example:

var expect = require('chai').expect;
var calculator = require('./StringCalculator');

// CANNOT break on the line below
describe('add', function() {
  // CANNOT break on the line below
  it('returns 0 for empty string', function() {
    // CAN break on the line below
    expect(calculator.add('')).to.equal(0);
  });
});

EDIT2

The problem is fixed in my fork of node-inspector: https://github.com/strongloop/node-inspector. You can set breakpoints anywhere in your specfiles immediately after the node-inspector UI is loaded in your browser.




回答2:


See my answer to a related question here: https://stackoverflow.com/a/29351654/3304034 for a decent enough work around



来源:https://stackoverflow.com/questions/16967499/in-debug-mode-mocha-doesnt-stop-on-debugger-statements-in-a-spec-file-when-usi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!