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
- Stop using Node `--inspect` option in the docs on debugging The technique described in the instructions does not pick up breakpoints set with `debugger` statements. This issue is being tracked by htt... — committed to bent/jest by bent-sportsbet 8 years ago
- Stop using Node `--inspect` option in the docs on debugging (#1998) The technique described in the instructions does not pick up breakpoints set with `debugger` statements. This issue is being trac... — committed to jestjs/jest by bent 8 years ago
- Stop using Node `--inspect` option in the docs on debugging (#1998) The technique described in the instructions does not pick up breakpoints set with `debugger` statements. This issue is being trac... — committed to okonet/jest by bent 8 years ago
- Stop using Node `--inspect` option in the docs on debugging (#1998) The technique described in the instructions does not pick up breakpoints set with `debugger` statements. This issue is being trac... — committed to nickpresta/jest by bent 8 years ago
- fix tests to pass in jsdom env, debugger mode doesn't work - see https://github.com/facebook/jest/issues/1652 — committed to HubSpot/draft-convert by deleted user 7 years ago
- jest: prefer old V8 debugger protocol by default (because of issues with --inspect like https://github.com/facebook/jest/issues/1652), controlled by jest.debugger.use.inspect registry key — committed to JetBrains/intellij-community by segrey 7 years ago
- Revert ":fire: drop --debug*" per https://tinyurl.com/k546ycf This reverts commit 01ab6c9ab743d7de5b481d42fcb760188c117620. See https://github.com/facebook/jest/issues/1652 for more. --inspect isn't... — committed to cdaringe/create-react-app by cdaringe 7 years ago
- fix(debug): disable inspect support per facebook/jest#1652 — committed to cdaringe/create-react-app by cdaringe 7 years ago
- Stop using Node `--inspect` option in the docs on debugging (#1998) The technique described in the instructions does not pick up breakpoints set with `debugger` statements. This issue is being trac... — committed to tushardhole/jest by bent 8 years ago
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)
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:
Install module
jest-environment-node-debug
or using npm:
npm install jest-environment-node-debug --save-dev
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 node10.8.0
:@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:
Having installed
jest-environment-node-debug
, I was able to produce a workinglaunch.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):
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
: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
Then just like run any unit test. crtl/command + shift + F10/F9
Breakpoints don’t hit for me.
See the example project here: https://github.com/sparebytes/jest-debug-example
This is the launch config:
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:
Will break three times with
node debug index.js
but only twice withnode --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:
Source: https://jestjs.io/docs/en/troubleshooting#tests-are-failing-and-you-don-t-know-why
on windows
@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.
and
However, this debugger line will not cause a stop:
This line will:
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 workIt 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 usingjest-environment-node-debug-fixed
. 😢Edit: However, I got it to work using bugger https://github.com/buggerjs/bugger
@DanielHoffmann You did it wrong 😃
Basically the requisites are
@NikhilVerma The global variables document, window, and the other browser globals aren’t present when jest-environment-node-debug is used.
@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#L5edit - just tested, you need to type
repl
before typingtest
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?