Debug Jest tests for Lerna project in VS Code

Viewed 1395

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?

2 Answers

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}"
    }
  ]
}

Lerna allows you to have npm packages in the root node_modules that are hoisted to your packages. That said, if you have jest as a dependency in your root package.json file you should be able to run tests in every package.

I have my launch.json configured as below. The first config runs all tests, and the second runs the test that is currently opened in VS Code.

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