jest: debugger; and breakpoints don't work with node --inspect

Currently breakpoints can be set for use with node debug using the keyword debugger;, but any breakpoints set with debugger; when using node --inspect are ignored. This only applies to the test code; breakpoints set with debugger; within Jest or other node_module libraries are honored.

I set up a test repository to demonstrate the difference: https://github.com/snapwich/jest-inspect. Both methods can be tested by pulling the repo and running the following:

npm install

// works
node debug --debug-brk ./node_modules/.bin/jest -i

// doesn't honor "debugger;"
node --inspect --debug-brk ./node_modules/.bin/jest -i

Also, after the initial run-through with node --inspect, breakpoints can be applied to the code in the Sources tab. Any breakpoints placed in test code (such as __tests__/jest.js in jest-inspect repo above) will be ignored, but breakpoints placed in any other files (Jest or other node_modules code) will work correctly when the suite is rerun.

node --inspect is only available in node v6.3.0+ and I’m running on OSX using node v6.4.0 and npm v3.10.3

/cc @kentcdodds

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 92
  • Comments: 78 (26 by maintainers)

Commits related to this issue

Most upvoted comments

node --inspect --inspect-brk node_modules/bin/jest should work with node 8.4.0 out of the box. I’m so happy this finally works, it was one major issue that was making it hard to debug JavaScript tests.

Thanks for the steps @NikhilVerma. I have yet to try it out myself, but I thought I’d mention that I hope this is high on the list of things for the Jest team to work on. I imagine that users of Jest (including those at Facebook) would very much like a better debugging experience 😃 With how easy everything else is with Jest, this is uncharacteristically hard (and should I say… painful 🙀 😉).

FYI, this issue should be resolved as of Node 8.4.0 (https://github.com/nodejs/node/pull/14465).

Any news on this?

( For people that are interested, I use VS Code with this launch JSON)

 {
      "name": "Run Tests With Debugger (slower, use npm run watch for normal work)",
      "type": "node",
      "request": "launch",
      "port": 5858,
      "address": "localhost",
      "stopOnEntry": false,
      "runtimeExecutable": null,
      "runtimeArgs": [
        "--debug-brk",
        "./node_modules/.bin/jest",
        "-i"
      ],
      "cwd": "${workspaceRoot}"
    }

screen shot 2016-12-07 at 13 16 12

IntelliJ Platform (IntelliJ IDEA, WebStorm) will support Jest in the next 2017.1 release (https://youtrack.jetbrains.com/issue/WEB-14979). And v8 inspector is used by default. Will be strange if you can run, but cannot debug.

But it is not Jest issue — it is NodeJS issue. And there is a simple workaround — if you want to debug the only test, you don’t need to bother about global state. So, I implemented special test environment for this purpose:

  1. Install module jest-environment-node-debug

    yarn add jest-environment-node-debug --dev
    

    or using npm: npm install jest-environment-node-debug --save-dev

  2. Run using flag --env jest-environment-node-debug

Debug works 😃

(Line position is not correct sometimes in the IntelliJ Platform NodeJS Debugger if you use TypeScript and Babel preprocessor together — it is unrelated issue and will be fixed in the 2017.1).

Adding this for anyone having this issue while using the --watch option - debugger statements are ignored with --watch, however adding --no-cache fixes the issue. I can confirm that the following works consistently on node 10.8.0:

node --inspect-brk node_modules/.bin/jest --runInBand --no-cache --watch

@sparebytes in your console, check your node --version, make sure it’s 8.4. The following launch config worked for me with your same specs:

  {
    "name": "Debug Jest tests",
    "type": "node",
    "request": "launch",
    "program": "${workspaceRoot}\\node_modules\\jest\\bin\\jest.js",
    "args": [
      "--runInBand",
      "--no-cache"
    ],
    "runtimeArgs": [
      "--inspect-brk"
    ],
    "cwd": "${workspaceRoot}",
    "protocol": "inspector",
    "console": "integratedTerminal"
  }

Having installed jest-environment-node-debug, I was able to produce a working launch.json config for vscode, using inspector protocol with node 8.

If anyone is interested, this is the minimal configuration needed (assuming port and address are standard):

{
  "name": "Debug Jest",
  "type": "node",
  "request": "launch",
  "runtimeArgs": [
    "--inspect-brk",
    "./node_modules/.bin/jest",
    "-i",
    "--env",
    "jest-environment-node-debug"
  ],
  "cwd": "${workspaceRoot}",
  "protocol": "inspector",
  "console": "integratedTerminal"
}

This issue is the biggest reason why I can’t switch to Jest. Any updates or ideas on how to make progress?

For those using create-react-app / react-scripts:

./node_modules/.bin/react-scripts --inspect-brk test --env=jsdom --runInBand

Make sure you’re on react-scripts@^1.011!

I got @develar’s tool working with node --inspect-brk ./node_modules/.bin/jest --runInBand --env jest-environment-node-debug.

For anyone looking for clarification, I was able to hit debugger statements in my code and test files in Node 7.4.0, Jest 18.x, and using @develar’s jest-environment-node-debug package (from this comment) with this command:

node --debug-brk --inspect ./node_modules/.bin/jest -i --env jest-environment-node-debug

Seems like jest should support the node v6+ --inspect and --inspect-brk commands. Hopefully this will come soon to jest as it appears many people are struggling to debug their unit tests (me included).

My solution for runing and debugging single Jest test in IntelliJ Idea.

Node version: v6.9.4

single jest runner

Then just like run any unit test. crtl/command + shift + F10/F9

debugger

Breakpoints don’t hit for me.

  • Windows 10
  • Node 8.4
  • VSCode 1.15.1
  • Jest 20

See the example project here: https://github.com/sparebytes/jest-debug-example

This is the launch config:

{
    "name": "Debug Jest tests",
    "type": "node",
    "request": "launch",
    "sourceMaps": true,
    "runtimeArgs": [
        "--inspect-brk",
        "--nolazy",
        "./node_modules/jest/bin/jest.js",
        "-i"
    ],
    "cwd": "${workspaceRoot}",
    "protocol": "inspector",
    "console": "integratedTerminal"
}

I’m going to close this issue because it isn’t actionable from our side. Some workarounds were documented above, that may or not work for you. Once this is fixed in node/v8, it will automatically start working eventually.

This NodeJS issue looks related: https://github.com/nodejs/node/issues/7593.

Repro case is minimal:

var vm = require('vm');
new vm.Script('debugger;').runInContext(vm.createContext());
debugger;

Will break three times with node debug index.js but only twice with node --inspect --debug-brk index.js

For those still stuck with this problem, debugging in Chrome DevTools works with the debugger statement. Not an ideal solution, but a decent workaround:

To debug in Google Chrome (or any Chromium-based browser), simply open your browser and go to chrome://inspect and click on “Open Dedicated DevTools for Node”, which will give you a list of available node instances you can connect to. Simply click on the address displayed in the terminal (usually something like localhost:9229) after running the above command, and you will be able to debug Jest using Chrome’s DevTools.

The Chrome Developer Tools will be displayed, and a breakpoint will be set at the first line of the Jest CLI script (this is done simply to give you time to open the developer tools and to prevent Jest from executing before you have time to do so). Click the button that looks like a “play” button in the upper right hand side of the screen to continue execution. When Jest executes the test that contains the debugger statement, execution will pause and you can examine the current scope and call stack.

Source: https://jestjs.io/docs/en/troubleshooting#tests-are-failing-and-you-don-t-know-why

on windows

        {
            "name": "Debug Jest",
            "type": "node",
            "request": "launch",
            "runtimeArgs": [
                "--inspect-brk",
                "./node_modules/jest/bin/jest.js",
                "-i",
                "--env",
                "jest-environment-node-debug"
            ],
            "cwd": "${workspaceRoot}",
            "protocol": "inspector",
            "console": "integratedTerminal",
            "sourceMaps": true
        }

@iammerrick @trodrigues @trxcllnt @snapwich Please try this npm module jest-environment-node-debug

I’ve made some changes which has fixed node debugging for me using Node v7 and Jest 18.

Also the way I use it by adding "testEnvironment": "jest-environment-node-debug" in my Jest config.

Just FYI I used devtool for debugging

EDIT: Sorry I was too early, I still see some issues (trying to fix them) EDIT2: Never mind I was trying to use the same environment to run my tests locally which failed because it needs the node debug environment EDIT3: Thanks @develar for the permission to publish as the original module

Hi guys, any update on this issue?

I can get the debugger to spawn and hit debugger lines in node 8.

"test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand -t",

and

nvm exec 8 yarn test:debug

However, this debugger line will not cause a stop:

describe.skip('With Enzyme', () => {
  it('ArticleSidebar component displays', () => {
    debugger;

This line will:

// this code is in not in tests
import { config } from './nextConfig';
let log, logError;
debugger; 

So the moral of the story for me appears to be: don’t expect debugger breaks to be hit if they are in Jest test code. Why? Maybe someone smarter than me wants to figure that out 😸

Looks like it not the jest problem, but nodejs problem. I Having the same issue with debugging my application as well. I’m using node v8.11.3

@yevhenchmykhunep did you give this a shot?

./node_modules/.bin/react-scripts --inspect-brk test --env=jsdom --runInBand

By @Timer

Is this issue still being worked on?

@bsr203 I just tested on node v7.6.0 and breakpoints and debugger;statements on tests still do not work

It seems that node-inspector is not maintained anymore and does not support node 7.*. When I use node --debug-brk --inspect ./node_modules/react-scripts/node_modules/.bin/jest -i With CRA and node 7.1 it seems to load correctly in the dev tools and than hang with “waiting for debugger to disconnect” (even though execution is free)

I wanted to report back that we encountered the same problem as mentioned by @brien-givens

As he said adding --no-cache solves the problem for us (details see here: https://github.com/facebook/jest/issues/1652#issuecomment-412692363)

@brien-givens thanks for sharing your solution 😃

It’s funny, it seems like i have better luck here if I turn OFF Node Inspector Manager and use chrome://inspect instead.

This is not a Jest issue. This is a bug in v8/node, see https://github.com/nodejs/node/issues/7593 – please express your concerns there and ask the node or v8 teams to fix this within vm/contextify.

I still get ReferenceError: window is not defined even after using jest-environment-node-debug-fixed. 😢

Edit: However, I got it to work using bugger https://github.com/buggerjs/bugger

@DanielHoffmann You did it wrong 😃

git clone git@github.com:NikhilVerma/jest-environment-node-debug-fixed.git
cd jest-environment-node-debug-fixed
npm install
npm run devtool

Basically the requisites are

  • Node v7.5.0 at least
  • Jest 17+
  • Devtool installed
  • Jest CLI ran with “-i” option
  • jest-environment-node-debug package (1.0 if you have jest17 and 2.0 if you have jest18)
  • Make a clone of your jest config and add “testEnvironment”: “jest-environment-node-debug” and use that config to run jest (So that your normal tests aren’t broken)

@NikhilVerma The global variables document, window, and the other browser globals aren’t present when jest-environment-node-debug is used.

test('foo', () => {
  document.body.textContent = '123';
});

@trxcllnt tried that, unfortunately doesn’t work for me 😦

@maximderbin not sure why you’re getting that ReferenceError, test should be defined just a few lines above there: https://github.com/snapwich/jest-inspect/blob/master/__tests__/jest.js#L5

edit - just tested, you need to type repl before typing test so the code runs in the proper context.

For me the issue was with two *.test.js files with the same name (located in different folders). When I removed one of them, debugger was hit in the other without problems. Configuration: "name": "Debug Jest Test", "type": "node", "request": "launch", "program": "${workspaceRoot}/node_modules/jest/bin/jest", "args": [ "${fileBasenameNoExtension}", ], "console": "integratedTerminal"

This extension has helped me: https://chrome.google.com/webstore/detail/nodejs-v8-inspector-manag/gnhhdgbaldcilmgcpfddgdbkhjohddkj/related?hl=en

It detects node --inspect and opens devTools in Chrome for that websocket. debugger statements and breakpoints set manually on devtools has worked!

I think we need to create a repro without Jest and submit an issue to the node.js bugtracker.

maybe it is possible to run tests without vm context in debug mode?

Was this ever working before?

I don’t think vm.runInContext supports the debugger protocol.

This article mentions port forwarding to support debugging VM code. Not sure if that’s relevant.

Beyond that, maybe it’s possible to use websockets within the VM (to support the debugger protocol)?

What do you think, @cpojer?