Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode debugger not working in Jest tests

I'm struggling to get the Visual Studio Code debugger working in with Jest tests.

Here is my launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Jest All",
      "program": "${workspaceFolder}/node_modules/jest/bin/jest",
      "args": ["--runInBand"],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "sourceMaps": true
    }
  ]
}

Here are my Jest tests with a couple of breakpoints:

enter image description here

When I hit the green play button to run the tests with the debugger, the breakpoints are never hit.

Any help would be appreciated

like image 899
Carl Rippon Avatar asked Jun 24 '18 11:06

Carl Rippon


People also ask

Can you debug jest tests?

You can use Chrome DevTools to debug Jest tests. Now click the inspect link under "Remote Target" to open Chrome DevTools. Note that you probably need to add the source code folder to the workspace in chrome-devtools, so as to be able to set breakpoints. Now you can press F8 to start debugging.

How do you debug test cases in react?

Line 18 is a must to use Chrome DevTools for debugging unit test cases. Here the executable is react-app-rewired . It can be invoked by typing npm run test:debug .


1 Answers

Personnally I use this configuration

{
  "name": "Launch e2e test",
  "type": "node",
  "request": "launch",
  "env": {
    "NODE_ENV": "test"
  },
  "args": [
    "--colors",
    "--config=${workspaceFolder}/jest-e2e.config.js",
    "--runInBand",
    "--coverage"
  ],
  "runtimeArgs": [
    "--nolazy"
  ],
  "windows": {
    "program": "${workspaceFolder}/node_modules/jest/bin/jest",
  },
  "outputCapture": "std",
  "internalConsoleOptions": "openOnSessionStart"
}

Change jest-e2e.config.js by your configuration file. And remove or keep coverage

Like Laura Slocum said you will certainly have problem with line number. In my case personnaly think that the problem come from the jest configuration, the transform :

  transform: {
    "^.+\\.(t|j)s$": "ts-jest"
  },
like image 196
xrobert35 Avatar answered Oct 17 '22 22:10

xrobert35