jest-community / vscode-jest

The optimal flow for Jest based testing in VS Code
MIT License
2.84k stars 294 forks source link

[BUG] Cannot debug tests #1188

Open eliasm307 opened 1 month ago

eliasm307 commented 1 month ago

Describe the bug

I am getting a console error when trying to debug tests

To Reproduce Steps to reproduce the behavior:

  1. Right click test gutter icon
  2. Click debug test

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Environment (please complete the following information):

Prerequisite

Additional context Add any other context about the problem here.

image

Example terminal output:

/usr/bin/env 'NODE_OPTIONS= --require /snap/code/172/usr/share/code/resources/app/extensions/ms-vscode.js-debug/src/bootloader.js  --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS=:::{"inspectorIpc":"/tmp/node-cdp.446728-e19dc2e4-1.sock","deferredMode":false,"waitForDebugger":"","execPath":"/home/emangoro/.nvm/versions/node/v20.15.1/bin/node","onlyEntrypoint":false,"autoAttachMode":"always","fileCallback":"/tmp/node-debug-callback-ed58fb85f20b996d"}' /home/emangoro/.nvm/versions/node/v20.15.1/bin/node ./node --expose-gc \'node_modules/.bin/jest\' --runInBand --detectOpenHandles --runInBand --watchAll=false --testNamePattern test$ --runTestsByPath /home/repo/file.spec.ts 
Debugger attached.
Waiting for the debugger to disconnect...
node:internal/modules/cjs/loader:1148
  throw err;
  ^
Error: Cannot find module '/home/emangoro/git-repos/front/node'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)
    at Module._load (node:internal/modules/cjs/loader:986:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
    at node:internal/main/run_main_module:28:49 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
Node.js v20.15.1

The fastest (and the most fun) way to resolve the issue is to submit a pull request yourself. If you are interested, please check out the contribution guide, we look forward to seeing your PR...

eliasm307 commented 1 month ago

I'm having to use the jest-runner extension to debug tests, it produces the following CLI command which works:

/usr/bin/env 'NODE_OPTIONS= --require /snap/code/172/usr/share/code/resources/app/extensions/ms-vscode.js-debug/src/bootloader.js  --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS=:::{"inspectorIpc":"/tmp/node-cdp.446728-f9dba5ff-4.sock","deferredMode":false,"waitForDebugger":"","execPath":"/.../.nvm/versions/node/v20.15.1/bin/node","onlyEntrypoint":false,"autoAttachMode":"always","fileCallback":"/tmp/node-debug-callback-617b8ed7c3b63ffa"}' /home/..../.nvm/versions/node/v20.15.1/bin/node node_modules/.bin/jest /home/..../file.spec.ts -c /home/..../jest.config.ts -t test --runInBand --detectOpenHandles --runInBand
connectdotz commented 2 weeks ago

It’s possible that the extension is attempting to auto-generate a debug configuration using your jest.jestCommandLine but encountered an error; it’s likely due to your jestCommandLine having mixed Node and Jest commands. A straightforward solution might be to create a custom debug configuration manually:

After the initial debug attempt, the extension typically outputs the generated debug configuration in the terminal window. You can use that output to create a custom configuration. Based on the information you have provided, your debug configuration might look something like this:

{
  "type": "node",
  "name": "vscode-jest-tests.v2.your-folder",
  "request": "launch",
  "args": [
    "--runInBand",
    "--detectOpenHandles",
    "--watchAll=false",
    "--testNamePattern",
    "${jest.testNamePattern}",
    "--runTestsByPath",
    "${jest.testFile}"
  ],
  "cwd": "absolute-path-to-your-folder",
  "console": "integratedTerminal",
  "internalConsoleOptions": "neverOpen",
  "disableOptimisticBPs": true,
  "program": "absolute-path-to-your-folder/node_modules/.bin/jest",
  "runtimeArgs": ["--expose-gc"]
}

In this configuration:

Let me know if this helps resolve the issue.

eliasm307 commented 2 weeks ago

@connectdotz thanks for the response, the config you provided fixes the issue and I'm able to debug via the extension with the node flags applied

Is there any plan to allow the node flags to be configured via extension settings to avoid a custom debug config?