Search code examples
visual-studio-codejestjslerna

Debug Jest tests for Lerna project in VS Code


I'm wanting to debug specific Jest tests in VS code for a project that uses Lerna, so there are multiple folders each with their own node_modules folder. With help from this answer I've got the following launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Jest watch",
      "program": "${workspaceRoot}/my/specific/module/node_modules/jest/bin/jest.js",
      "args": ["--verbose", "-i", "--no-cache", "--watchAll"],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "cwd": "${workspaceFolder}/my/specific/module"
    }
  ]
}

The problem is that I've had to put the specific path to the module in the launch config so I have to change it every time I want to debug something else.

Is there a better way to do this? Maybe use the folder that's selected in the Explorer? Maybe have some way of launching debug by right-clicking the test file?


Solution

  • With much thanks to dlac for the idea, I now have a working launch config:

    {
      "version": "0.2.0",
      "configurations": [
        {
          "type": "node",
          "request": "launch",
          "name": "Jest watch",
          "program": "${workspaceFolder}/node_modules/.bin/jest",
          "args": ["--verbose", "-i", "--no-cache", "--watchAll"],
          "console": "integratedTerminal",
          "internalConsoleOptions": "neverOpen",
          "windows": {
            "program": "${workspaceFolder}/node_modules/jest/bin/jest"
          },
          "cwd": "${fileDirname}"
        }
      ]
    }