Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debugger; and breakpoints don't work with node --inspect #1652

Closed
snapwich opened this issue Sep 8, 2016 · 78 comments
Closed

debugger; and breakpoints don't work with node --inspect #1652

snapwich opened this issue Sep 8, 2016 · 78 comments

Comments

@snapwich
Copy link
Contributor

snapwich commented Sep 8, 2016

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

@maximderbin
Copy link
Contributor

run into the same problem today

@maximderbin
Copy link
Contributor

maximderbin commented Sep 8, 2016

Also in your example if I run npm run debug

  6
  7     // break won't be triggered
> 8     debugger;
  9
 10     expect(test).toBe(true);
test
ReferenceError: test is not defined

Am I doing something wrong or it doesn't have access to context?

@snapwich
Copy link
Contributor Author

snapwich commented Sep 8, 2016

@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.

@maximderbin
Copy link
Contributor

related issue facebook/create-react-app#594

@cpojer
Copy link
Member

cpojer commented Sep 9, 2016

This is interesting. Is it possible that node's --debug flag doesn't work when using the vm module to create a context?

@aleclarson
Copy link
Contributor

Any news on this?

@aleclarson
Copy link
Contributor

aleclarson commented Oct 3, 2016

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?

@gbiryukov
Copy link

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

@damonbauer
Copy link

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

@cpojer
Copy link
Member

cpojer commented Oct 20, 2016

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

@Met48
Copy link
Contributor

Met48 commented Oct 20, 2016

This NodeJS issue looks related: nodejs/node#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

@kmcq
Copy link

kmcq commented Oct 24, 2016

@cpojer do you think it's a good idea to change the content of http://facebook.github.io/jest/docs/troubleshooting.html#tests-are-failing-and-you-don-t-know-why to reflect that this doesn't work yet? I'm happy to submit a PR for this if that would be helpful -- what should the section say? Delete it? Say "coming soon"?

(I love Jest by the way! It makes React testing even more awesome)

bent pushed a commit to bent/jest that referenced this issue Oct 26, 2016
The technique described in the instructions does not pick up
breakpoints set with `debugger` statements. This issue is being
tracked by jestjs#1652, but
it's unclear how long it will take to resolve as it's actually
a Node issue.

As a work-around, have updated docs to describe connecting an
external debugger to the Node process.
@bent
Copy link
Contributor

bent commented Oct 26, 2016

I've created a PR for an update to the docs on this: #1998

cpojer pushed a commit that referenced this issue Oct 26, 2016
The technique described in the instructions does not pick up
breakpoints set with `debugger` statements. This issue is being
tracked by #1652, but
it's unclear how long it will take to resolve as it's actually
a Node issue.

As a work-around, have updated docs to describe connecting an
external debugger to the Node process.
okonet pushed a commit to okonet/jest that referenced this issue Nov 11, 2016
)

The technique described in the instructions does not pick up
breakpoints set with `debugger` statements. This issue is being
tracked by jestjs#1652, but
it's unclear how long it will take to resolve as it's actually
a Node issue.

As a work-around, have updated docs to describe connecting an
external debugger to the Node process.
nickpresta pushed a commit to nickpresta/jest that referenced this issue Nov 15, 2016
)

The technique described in the instructions does not pick up
breakpoints set with `debugger` statements. This issue is being
tracked by jestjs#1652, but
it's unclear how long it will take to resolve as it's actually
a Node issue.

As a work-around, have updated docs to describe connecting an
external debugger to the Node process.
@guy-kdm
Copy link

guy-kdm commented Nov 15, 2016

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)

@thomasklein
Copy link

Hi guys, any update on this issue?

@trodrigues
Copy link

Thanks to all who got this to work 👏

@WickyNilliams
Copy link

WickyNilliams commented Aug 16, 2017

So is there anything in particular that needs to be done beyond installing latest node v8.4.0? Could someone post their vscode launch.json for this?

This vscode launch config works for me :)

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

@trodrigues
Copy link

You just need to run it in "the new way". That is, with inspect, --inspect or --inspect-brk. See https://nodejs.org/en/docs/inspector/ for more info on the various ways of running it and things that can connect to it.

The aforementioned jest-environment-node-debug is not necessary.

@cpojer
Copy link
Member

cpojer commented Aug 16, 2017

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.

@Timer
Copy link

Timer commented Aug 18, 2017

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!

@sparebytes
Copy link

sparebytes commented Aug 18, 2017

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"
}

@Phlow2001
Copy link

Phlow2001 commented Aug 19, 2017

@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"
  }

tushardhole pushed a commit to tushardhole/jest that referenced this issue Aug 21, 2017
)

The technique described in the instructions does not pick up
breakpoints set with `debugger` statements. This issue is being
tracked by jestjs#1652, but
it's unclear how long it will take to resolve as it's actually
a Node issue.

As a work-around, have updated docs to describe connecting an
external debugger to the Node process.
@SudoPlz
Copy link

SudoPlz commented Apr 16, 2018

Still doesn't work for me on 8.4.0 and I've tried everything.

It looks like the break points become red (get activated) after the tests have finished.. Any ideas?

@ghost
Copy link

ghost commented Jun 27, 2018

+1
It does not work for me too

@robophil
Copy link

@yevhenchmykhunep did you give this a shot?

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

By @Timer

@FossPrime
Copy link

I'm having the issue on node v10.5.0 and [email protected] node --inspect --inspect-brk ./node_modules/jest/bin/jest.js

The break on first line works, but the debugger calls don't stop :(

@ghost
Copy link

ghost commented Jun 30, 2018

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

@jcollum
Copy link
Contributor

jcollum commented Jul 5, 2018

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 😸

@jchani
Copy link

jchani commented Jul 9, 2018

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

@FossPrime
Copy link

@jchani even with this method, debug statements inside tests will still not break. As @jcollum pointed out. Even with break on first line enabled. Break points inside tests is the type we all want more than any other. Debugging why a test stopped working is a pita without this ability.

vuematerial/vue-material#1840 @Samuell1 why do you want to stick with jest 22 when we have 4 tests failing on dev... imo no commit should ever be allowed with failing tests.

@FossPrime
Copy link

FossPrime commented Jul 27, 2018

For Vue users: if your debugger statements aren't working... try moving them to your component instead of the jest code, do as little as possible in jest and just check the state of your fixtures... at least until the nodejs team fixes this issue.

My debugger calls are ininput-speed.vue. I realise this is not ideal, but it beats not having debugging or forgoing tests because they are too hard to build.

import Vue from 'vue'
import { mount } from 'avoriaz'
import f1 from '../fixtures/input-speed.vue'

test('should work with phones autocorrect', async () => {
  const wrapper = await mount(f1, {})
  const vm = wrapper.vm

  await wrapper.vm.$nextTick()

  // should work with android autocorrect without waiting
  expect(vm.form.server).toBe('autocorrect')
  expect(vm.noBind).toBe('Not data binded')  // Should work without data binding
})

@brien-givens
Copy link

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

@prufrock123
Copy link

prufrock123 commented Aug 14, 2018

for node 6.x, instead of node --inspect-brk, try node inspect (note, the lack of hyphens)

nvm

@bernardoadc
Copy link

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!

@jcollum
Copy link
Contributor

jcollum commented Sep 18, 2018

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

@june07
Copy link

june07 commented Oct 19, 2018

@jcollum As the developer of NiM... anything, in particular, I should be aware of?

@jcollum
Copy link
Contributor

jcollum commented Oct 19, 2018

Sorry, I don't have anything specific here. Sometimes hitting a breakpoint in a file that's behind a sourcemap is just flaky. Next time I need to debug a jest test I'll give NIM another shot.

@tschoartschi
Copy link

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: #1652 (comment))

@brien-givens thanks for sharing your solution :-)

@yury-kozlov
Copy link

yury-kozlov commented May 21, 2019

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"

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests