9

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

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Carl Rippon
  • 4,553
  • 8
  • 49
  • 64
  • I don't know if this would be useful to anyone, but I had an issue where my debugger started acting up out of the blue. I tried reloading vscode with no luck, but completely closing it and reopening it seemed to have done the trick for me. – Felipe Centeno Mar 15 '23 at 18:01

6 Answers6

7

Personally 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"
  },
brandonscript
  • 68,675
  • 32
  • 163
  • 220
xrobert35
  • 2,488
  • 1
  • 16
  • 15
3

This configuration let's me debug the jest test. Unfortunately hitting a breakpoint in the component does not show the correct line, even though it is stepping through the correct code. I believe this is probably a VSCode error though

{
  "name": "Jest", // This is the configuration name you will see in debug sidebar
  "type": "node",
  "request": "launch",
  "port": 5858,
  "address": "localhost",
  "stopOnEntry": false,
  "runtimeExecutable": null,
  "env": {
    "NODE_ENV": "development"
  },
  "console": "integratedTerminal",
  "preLaunchTask": "compile",
  "runtimeArgs": [
    "--inspect-brk", // node v8 use debug-brk if older version of node
    "${workspaceRoot}/node_modules/.bin/jest",
    "--watch",
    "--bail",
    "--runInBand"
  ],
  "cwd": "${workspaceRoot}"
},
Laura Slocum
  • 7,696
  • 2
  • 11
  • 9
1

I had the same problem with line numbers being off. In the source file I had almost 30 lines of requires, and the test file that loaded in the debugger added a blank space between each require. So the file that got loaded in vscode was about 60 lines longer.

I found this post that fixed my problem: Debugging Jest Tests in VS Code: Breakpoints Move

0

The problem for me was the value of the program attribute in launch.json. If your launch.json is as follows:

        "program": "${workspaceFolder}/node_modules/jest/bin/jest"

Check if ${workspaceFolder}/node_modules/jest/bin/jest is actually valid. For me, the node_modules did not exist here, but in a subdirectory of workspaceFolder.

Ojasvi Monga
  • 4,437
  • 2
  • 18
  • 35
0

The following is the only launch.config that worked for me after trying out everything else :|

{
    "version": "0.2.0",
    "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Jest",
      "program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
      "args": [
          "-i"
      ],
      "skipFiles": [
          "<node_internals>/**/*.js", "node_modules",
      ]
    }
  ]
}
Ε Г И І И О
  • 11,199
  • 1
  • 48
  • 63
0

If you are using transformers like babel or swc to transform your tests before running the actual tests, the debugger in vscode may not work.

For me I'll just use the debugger.

yqrashawn
  • 413
  • 5
  • 8