31

Does anyone have any idea if this is possible? Most of the sample for node-inspector seemed geared toward debugging an invoked webpage. I'd like to be able to debug jasmine-node tests though.

j03m
  • 5,195
  • 4
  • 46
  • 50

3 Answers3

32

In short, just debug jasmine-node:

node --debug-brk node_modules/jasmine-node/lib/jasmine-node/cli.js spec/my_spec.js

If you look at the source of the jasmine-node script, it just invokes cli.js, and I found I could debug that script just fine.

I wanted to use node-inspector to debug a CoffeeScript test. Just adding the --coffee switch worked nicely, e.g.

node --debug-brk node_modules/jasmine-node/lib/jasmine-node/cli.js --coffee spec/my_spec.coffee
Ian
  • 977
  • 8
  • 14
  • Actually, this seems to work fine. Just press 'Resume script execution' button after loading debugger. This takes you to the first breakpoint. I'm not sure why my first tests exited instead. – Rich Apodaca Aug 17 '13 at 15:59
  • 1
    it is important NOT to specify --autotest command line option because it makes jasmine-node spawn child processes which are not debugable by default, without this option, it works nice – xorcus Feb 01 '15 at 21:46
  • 1
    if it helps others - i think with the updated node inspector: `node-debug /usr/local/lib/node_modules/jasmine-node/lib/jasmine-node/cli.js spec/` – rob_james Apr 22 '15 at 21:49
  • As of the date of my comment this works `node --inspect --debug-brk -i ./node_modules/jasmine-node/lib/jasmine-node/cli.js test-jasmine/` as @xorcus mentioned you can't use --autotest. – Jerinaw Jul 20 '17 at 20:10
9

I ended up writing a little util called toggle:

require('tty').setRawMode(true);
var stdin = process.openStdin();

exports.toggle = function(fireThis)
{
    if (process.argv.indexOf("debug")!=-1)
    {
        console.log("debug flag found, press any key to start or rerun. Press 'ctrl-c' to cancel out!");
        stdin.on('keypress', function (chunk, key) {
            if (key.name == 'c' && key.ctrl == true)
            {
                process.exit();
            }
            fireThis();
        });
    }
    else
    {
        console.log("Running, press any key to rerun or ctrl-c to exit.");
        fireThis();
        stdin.on('keypress', function (chunk, key) {
            if (key.name == 'c' && key.ctrl == true)
            {
                process.exit();
            }
            fireThis();
        });



    }
}

You can drop it into your unit tests like:

var toggle = require('./toggle');

toggle.toggle(function(){

    var vows = require('vows'),
    assert = require('assert');

    vows.describe('Redis Mass Data Storage').addBatch({

....

And then run your tests like: node --debug myfile.js debug. If you run debug toggle will wait until you anything but ctrl-c. Ctrl-c exits. You can also rerun, which is nice.

w0000t.

j03m
  • 5,195
  • 4
  • 46
  • 50
4

My uneducated guess is that you'd need to patch jasmine, I believe it spawns a new node process or something when running tests, and these new processes would need to be debug-enabled.

I had a similar desire and managed to get expressso working using Eclipse as a debugger: http://groups.google.com/group/nodejs/browse_thread/thread/af35b025eb801f43

…but I realised: if I needed to step through my code to understand it, I probably need to refactor the code (probably to be more testable), or break my tests up into smaller units.

Your tests is your debugger.

timoxley
  • 5,156
  • 3
  • 36
  • 40
  • Apparently not true. See Ian's answer. – Rich Apodaca Aug 17 '13 at 16:00
  • I also believe node-inspector does not work with jasmine-node because jasmine-node spawns child processes and node-inspector does not handle that. Searching for a solution gives a hint that process.debug_port must be set to a unique value for each child process, but that requires tampering with jasmine-node source code, I guess – xorcus Feb 01 '15 at 21:13