3

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?

Nick Wilson
  • 4,959
  • 29
  • 42

2 Answers2

4

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}"
    }
  ]
}
Nick Wilson
  • 4,959
  • 29
  • 42
1

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",
      }
    }
  ]
dlac
  • 315
  • 2
  • 9
  • That config works, but we have a large number of modules and tests. With this config making a small change to the test I'm interested in results in every test for every module being run. I was hoping I could just target specific tests or modules a little more easily. – Nick Wilson Mar 02 '20 at 10:24
  • I edited the response to add a second configuration that will only run the test that is currently opened in VS Code. To run more than one test at once you probably need to use the terminal and pass an expression to match the tests that you want to run. – dlac Mar 02 '20 at 10:37
  • Close, but it doesn't seem to quite work. there's a big delay on starting it, not quite sure why, then I get a "0 tests found" message. The "relativeFile" idea has pointed me in the right direction though, I think I now have something that works, thanks. – Nick Wilson Mar 02 '20 at 11:05