简体   繁体   English

使用 node-inspector 调试 jasmine-node 测试

[英]Debugging jasmine-node tests with node-inspector

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.不过,我希望能够调试 jasmine-node 测试。

In short, just debug jasmine-node:简而言之,只需调试 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.如果您查看jasmine-node脚本的源代码,它只是调用cli.js ,我发现我可以很好地调试该脚本。

I wanted to use node-inspector to debug a CoffeeScript test.我想使用节点检查器来调试 CoffeeScript 测试。 Just adding the --coffee switch worked nicely, eg只需添加--coffee开关就可以很好地工作,例如

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

I ended up writing a little util called toggle:最后我写了一个叫做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.然后像这样运行你的测试:node --debug myfile.js debug。 If you run debug toggle will wait until you anything but ctrl-c.如果你运行调试切换将等到你除了 ctrl-c 之外的任何东西。 Ctrl-c exits. Ctrl-c 退出。 You can also rerun, which is nice.您也可以重新运行,这很好。

w0000t. w0000吨。

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.我没有受过教育的猜测是你需要修补 jasmine,我相信它会在运行测试时产生一个新的节点进程或其他东西,并且这些新进程需要启用调试。

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我有类似的愿望,并设法使用 Eclipse 作为调试器让 expressso 工作: 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.你的测试就是你的调试器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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