Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jestjs - Debugging Jest test cases using node-inspector

Is there a way to use node-inspector to debug unit tests with Jest? It would be nice to step through sometimes to see why tests are failing

I have tried a few ways

node-debug jest --runInBand 

from the as well as starting up the inspector first eg

$ node-inspector
$ node --debug-brk .
ode_modulesjest-cli --runInBand

and then navigate to http://127.0.0.1:8080/debug?port=5858

I have found that occasionally (1 in 10 or so times), the debugger opens the jest src files and its possible to debug them. Generally though, the scripts in the debugger only contain a 'no domain' folder and another irrelevant folder. Also the test scripts themselves are never loaded in the debugger.

Has anyone tried this before?

question from:https://stackoverflow.com/questions/25142173/debugging-jest-test-cases-using-node-inspector

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like the issue is that jest is using harmonize, which spawns a child process to ensure that the --harmony option is used.

harmonize/harmonize.js, lines 30-35

var node = child_process.spawn(process.argv[0], ['--harmony'].concat(process.argv.slice(1)), {});
node.stdout.pipe(process.stdout);
node.stderr.pipe(process.stderr);
node.on("close", function(code) {
    process.exit(code);
});

I was able to successfully debug jest tests (although tests that use JSX transforms are incredibly slow) by commenting out the code that jest is using to spawn the harmonized process.

node_modules/jest-cli/bin/jest.js, last lines of the file:

if (require.main === module) {
  //harmonize();                  <--- comment out
  _main(function (success) {
    process.exit(success ? 0 : 1);
  });
}

Then you can run:

$ node-debug --nodejs --harmony ./node_modules/jest-cli/bin/jest.js --runInBand

Jest relies on the --harmony flag being there, so that's why we need to add it back with --nodejs --harmony. We also add --runInBand so that the tests run in sequence, not in parallel.

This opens up the web debugger, and you can debug the tests, although it can be pretty slow to get to the test you want. Please comment if anyone knows a way to make this faster, and I'll update my answer.

You can add this to your package.json to make it easier to kick off:

...
    scripts: {
        "test": "jest",
        "test-debug": "node-debug --nodejs --harmony ./node_modules/jest-cli/bin/jest.js --runInBand"
    }
...

Of course, main concern with this solution is the editing of the jest source code. Will think about how to make a pull request to make this stick.

Created Github Issue Here: https://github.com/facebook/jest/issues/152


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...