1

I have a Jest test suite that I would like to debug using the VS Code interactive debugger. That works fine for normal NodeJS programs ("program": "foo.js" in launch.json). But the test suite (foo.test.js) isn't a self-contained program, it has to be run from Jest.

Is there a way to achieve this?

The particular code is here: https://gitlab.com/stevage/guess-projection

Steve Bennett
  • 114,604
  • 39
  • 168
  • 219

1 Answers1

2

Debugging standard Jest tests

This doc in Microsoft's vscode-recipies GitHub repo describes how to set up VS Code for debugging standard Jest tests.

The launch.json looks like this:

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

Debugging create-react-app Jest tests

If your application is a React app bootstrapped with create-react-app then the configuration is a little different since Jest is not launched directly and is described in this section of the create-react-app docs.

Brian Adams
  • 43,011
  • 9
  • 113
  • 111
  • Hmm, that first link seems perfect, but it doesn't work for me. VS Code goes into debugging mode, the tests get run...but the breakpoints don't get hit. – Steve Bennett Nov 30 '18 at 00:08
  • @SteveBennett huh, interesting. The launch.json from that link is what I've used in the past and I just tried it again with a clean project and seems to work fine in my environment. Can you share more about your Jest config and environment? – Brian Adams Nov 30 '18 at 03:58
  • Added link to my repo. The launch.json is a direct copy from that site. Running on OSX. – Steve Bennett Nov 30 '18 at 04:04
  • @SteveBennett I just took another look at this. I cloned your repo and copied in the `launch.json` from the link and was able to hit the breakpoints that I set in both the `Jest Current File` and `Jest All` configs. I'm running macOS 10.14.2, VSCode 1.30.0, NodeJS 10.14.2. Did you get it working in your environment? – Brian Adams Dec 14 '18 at 03:33
  • No, I didn't manage to get it to work, but glad it did for you. I'm macOS 10.12, VSCode 1.30.0, node 10.0.0. – Steve Bennett Dec 17 '18 at 05:34
  • 1
    Huh, just tested again, it works now. I did do a full computer restart between when I posted this question and now, maybe related to that? Or was perhaps using an older version of node at the time. Either way, awesome, it works now. – Steve Bennett Dec 17 '18 at 05:37