Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Cannot debug tests #1188

Open
eliasm307 opened this issue Oct 18, 2024 · 3 comments
Open

[BUG] Cannot debug tests #1188

eliasm307 opened this issue Oct 18, 2024 · 3 comments

Comments

@eliasm307
Copy link

eliasm307 commented Oct 18, 2024

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):

  • vscode-jest version: v6.3.1 (pre-release)
  • node -v: 20.16.0
  • npm -v: 10.8.2
  • jest version: 29.4.3
  • your vscode-jest settings:
    • jest.jestCommandLine? "node --expose-gc 'node_modules/.bin/jest' --runInBand --detectOpenHandles"
    • jest.runMode? { "coverage": true, "showInlineError": true, "type": "on-demand", "runAllTestsOnStartup": false, "deferred": false }
    • jest.outputConfig? { "revealOn": "demand", "revealWithFocus": "none", "clearOnRun": "none" },
    • anything else that you think might be relevant?
      "jest.useDashedArgs": true,
      "jest.useJest30": false,
  • Operating system: Ubuntu 24.04.1 LTS

Prerequisite

  • are you able to run jest from the command line? yes
  • where do you run jest CLI from? root directory of the project
  • how do you run your tests from the command line? via nx

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
Copy link
Author

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
Copy link
Collaborator

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:

  • Replace "your-folder", "absolute-path-to-your-folder" with the actual path to your project folder.
  • This setup should allow you to run Jest with your desired options, and the runtimeArgs section includes the --expose-gc flag if you need access to garbage collection.

Let me know if this helps resolve the issue.

@eliasm307
Copy link
Author

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants