Debugging Jest test cases using node-inspector

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)