2

Is there a way in node.js (ideally mocha too) to simulate the behavior of python

python -m pdb setup.py test

That will fallback to debug interface on if the program throws an uncaught exception.

EDIT

@robertklep suggested the undocumented breakOnException command which looks really awesome but not so much in practice:

test.js

var hello = {};
function testFunc () {
  anotherFunct();
}

function anotherFunct () {
  var b = hello.unfound;
}
testFunc();

And then it freezes at:

$ node debug test.js
< Debugger listening on port 5858
connecting to port 5858... ok
break in test.js:1
> 1 var hello = {};
  2 function testFunc () {
  3   anotherFunct();
debug> breakOnException
debug> c
debug> backtrace

EDIT2: test.js didn't yield an error in the first place. The above works fine.

fakedrake
  • 6,528
  • 8
  • 41
  • 64
  • 1
    You should probably use a "proper" remote debugger like [`node-inspector`](https://github.com/node-inspector/node-inspector), the REPL client is rather limited. FWIW, there's nothing wrong with the example you're showing, so the code runs and it's done. Perhaps that's why the backtrace is causing it to freeze (which obviously is still bad). – robertklep Apr 30 '15 at 16:46
  • You are right, I was careless. Thank you. – fakedrake Apr 30 '15 at 16:49

1 Answers1

1

Add this to your script:

process.on('uncaughtException', function() {
  debugger;
});

And start it with node debug script.js. That will drop you into the debugger on uncaught exceptions/errors.

To get this (somewhat) working for Mocha tests, you can add the lines above and follow the instructions here (specifically installing node-inspector and starting Mocha using --debug-brk).

EDIT: the Node debugger also has the ability to break on errors/exceptions:

$ node debug script.js
debug> breakOnException
debug> c
...
Community
  • 1
  • 1
robertklep
  • 198,204
  • 35
  • 394
  • 381
  • 1
    Well the debugger call is made out of the error context so that is not very useful at all... The point was to have a repl to inspect the circumstances under which the error occurred. Chrome also has that nice "break on error" button (and you can even get the async stack which is incredible) so I expected people would have ported it to node... – fakedrake Apr 30 '15 at 16:24
  • @fakedrake see my edit, Node also has something similar. – robertklep Apr 30 '15 at 16:28
  • Wow! How is this not documented? – fakedrake Apr 30 '15 at 16:34