Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest + Babel in VS Code debugger causes breakpoints to move

I am trying to debug a simple project using babel, jest, and vs code. When I set a breakpoint and then start debugging, my breakpoints jump around and are no longer where they were when I started. A sample repo can be seen here - https://github.com/RyanHirsch/starter-node

I've updated my launch.json to contain

{   "name": "Jest",   "type": "node",   "request": "launch",   "program": "${workspaceRoot}/node_modules/jest/bin/jest.js",   "stopOnEntry": false,   "args": ["-i", "${file}"],   "cwd": "${workspaceRoot}",   "runtimeExecutable": null,   "sourceMaps": true,   "protocol": "inspector" } 

And my .babelrc looks like:

{   "plugins": ["@babel/plugin-proposal-object-rest-spread"],   "sourceMaps": "inline",   "presets": [     [       "@babel/preset-env",       {         "targets": {           "node": "6.10"         }       }     ]   ] } 

I thought that the source map options were enough to get this to work but I was wrong. What needs to change in order to keep my breakpoints in their original locations? Specifically when trying to debug my tests.

==== Edit ====

Before my breakpoints are on test line 10 and implementation line 4:

Before Debugging

When I start debugging by selection my test file and then run Jest in the VS Code debug pane, my breakpoints jump to test line 9 and implementation line 6: During Debugging

Running on Node 9.6.1 with the following extensions:

DavidAnson.vscode-markdownlint EditorConfig.EditorConfig Orta.vscode-jest PKief.material-icon-theme PeterJausovec.vscode-docker Shan.code-settings-sync bungcip.better-toml dbaeumer.vscode-eslint dracula-theme.theme-dracula dzannotti.vscode-babel-coloring eamodio.gitlens esbenp.prettier-vscode gerane.Theme-FlatlandMonokai humao.rest-client mauve.terraform mikestead.dotenv mjmcloug.vscode-elixir mohsen1.prettify-json ms-vscode.Theme-MaterialKit ms-vscode.azure-account ms-vscode.cpptools ritwickdey.LiveServer sbrink.elm shanoor.vscode-nginx vscodevim.vim 
like image 401
RyanHirsch Avatar asked Feb 26 '18 23:02

RyanHirsch


People also ask

What happens when a breakpoint is reached when the debugger is enabled?

If a breakpoint is reached, or a signal not related to stepping occurs before count steps, stepping stops right away. Continue to the next source line in the current (innermost) stack frame. This is similar to step , but function calls that appear within the line of code are executed without stopping.

What does Babel Jest do?

Jest ships with one transformer out of the box – babel-jest . It will load your project's Babel configuration and transform any file matching the /\. [jt]sx?$/ RegExp (in other words, any .

How does debugger set breakpoints?

They work by patching the code you are trying to execute with an instruction that triggers a debug event in some fashion. This is accomplished by injecting a breakpoint instruction or when that is not supported by inserting an instruction that causes a fault that halts the core.


2 Answers

Got this issue (using jest 23.6.0), checked that this was a known issue on create react app, resolved on this pull request:

https://github.com/facebook/create-react-app/pull/3605/files

Applied the following config to my launch.json

{ "type": "node", "request": "launch", "name": "Jest All", "program": "${workspaceFolder}/node_modules/jest/bin/jest", "args": [ "test", "--runInBand", "--no-cache"
], "sourceMaps": false, "console": "integratedTerminal", "internalConsoleOptions": "neverOpen" },

Managed to get it breaking on the right breakpoints.

like image 73
Braulio Avatar answered Oct 11 '22 14:10

Braulio


@RyanHirsch ; just use retainLines": true alongside sourceMap: "inline" in your babelrc and It will work.

like image 31
Jefe_Spain Avatar answered Oct 11 '22 13:10

Jefe_Spain