3

How can I just use the simple node cli/repl debugger with Jest?

The Jest documentation uses node-inspector, but it is outdated/deprecated as of Node 6.3. I tried the recommended command anyway on Node 7.7.4:

node --debug-brk ./node_modules/.bin/jest --runInBand --no-cache [your_test_file]

But this simply hangs on the following (assumedly waiting on node-inspector):

(node:13452) DeprecationWarning: node --debug is deprecated. Please use node --inspect instead. Debugger listening on 127.0.0.1:5858

I added --inspect as specified by the warning, but even then execution doesn't stop on my debugger statement in Chrome DevTools.

This seems overly complicated for a very simple use case.

Joseph Siefers
  • 1,282
  • 1
  • 12
  • 23

2 Answers2

4

I found the following command works:

node debug ./node_modules/.bin/jest --runInBand --no-cache [your_test_file]

...but with some quirky behavior. When the debugger first stops you will see:

break in node_modules/jest/bin/jest.js:10
  8  */
  9
>10 'use strict';
 11
 12 require('jest-cli/bin/jest');
debug>

Apparently Jest always injects this breakpoint so that you have time to open Chrome DevTools (irrelevant in our case since we're only going to use cli/repl).

Continue past this breakpoint with c, and after a short time (without any indication of course that things are progressing along) you should see your breakpoint:

break in webpack/assets/react/components/presentation/Feed/Comments/Comment/commentSpec.jsx:12
 10     var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_comment2.default, { loading: true }));
 11
>12     debugger;
 13     expect(wrapper.find(_button2.default)).to.have.length(1);
 14   });
 debug>

The last weird thing is you need to type repl to inspect objects as described in Node Debugger and Inspecting variables using node's built-in debugger?

The combination of all these steps was not immediately obvious to me while reading the documentation, so I hope this answer helps someone get over the hurdle faster.

Community
  • 1
  • 1
Joseph Siefers
  • 1,282
  • 1
  • 12
  • 23
2

From node v8.4 the debugger keyword within the code is fixed for VM context. Refer this git comment.

1.Type debugger keyword in your Jest code:

describe('Testcase definition', () => {
   it('should be defined with subobjects', () => {
     debugger; // <-- This keyword breaks on Chrome inspect
     expect(true).toBe(true);
   });
});
  1. Command to Run:

    node --inspect-brk --inspect ./node_modules/.bin/jest -i tests/mytest.test.js

  2. Now open chrome://inspect/#devices on Chrome. Voila!

NiRUS
  • 3,901
  • 2
  • 24
  • 50