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] Fails to debug tests on Windows due to issue with paths #1152

Closed
kruncher opened this issue Jun 5, 2024 · 9 comments
Closed

[BUG] Fails to debug tests on Windows due to issue with paths #1152

kruncher opened this issue Jun 5, 2024 · 9 comments

Comments

@kruncher
Copy link

kruncher commented Jun 5, 2024

Describe the bug

I am seeing the following error when attempting to debug a unit test:

ies\\paths.test.ts ogram\ Files\\nodejs\\npm.cmd test -- --runInBand --watchAll=false --testNamePattern discoverFilesRecursively\\\(\\\)\ discovers\ all\ files\ with\ one\ file\ extension\$ --runTestsByPath C:\\Users\\me\\Desktop\\minimal-ts\\utiliti
633;C'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

Some useful points:

  • The tests run properly from the command line.
  • The extension is able to run tests fine.
  • The extension shows the pass/fail icons as appropriate.
  • It is only the "Debug Test" feature which is not working.

To Reproduce
Steps to reproduce the behavior:

  1. Go to the unit test file.
  2. Right-click on the green check icon to the left of the unit test.
  3. Select 'Debug Test'.
  4. See error

Expected behavior
Unit test should run and should break at breakpoint.

Environment (please complete the following information):

  • vscode-jest version:6.2.5
  • node -v: 22.2.0
  • npm -v or yarn --version: 10.7.0
  • jest or react-scripts (if you haven’t ejected) version: 29.7.0
  • your vscode-jest settings:
    "jest.shell": "",
    "jest.runMode": null,
    "jest.coverageColors": null
  • Operating system: Windows 10

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? "npm run test"
@connectdotz
Copy link
Collaborator

hi the command line didn't seem to be complete. Please repaste the full command line; and your debug config for jest if you have a customized one.

@olifer
Copy link

olifer commented Jun 20, 2024

I've encountered a similar issue on Windows due to the generated configuration by default. Try to define your test config in .vscode/launch.json as mentioned in these docs:

{
  "type": "node",
  "name": "vscode-jest-tests.v2",
  "request": "launch",
  "program": "${workspaceFolder}/node_modules/.bin/jest",
  "args": [
    "--runInBand",
    "--watchAll=false",
    "--testNamePattern",
    "${jest.testNamePattern}",
    "--runTestsByPath",
    "${jest.testFile}"
  ],
  "cwd": "${workspaceFolder}",
  "console": "integratedTerminal",
  "internalConsoleOptions": "neverOpen",
  "disableOptimisticBPs": true,
  "windows": {
    "program": "${workspaceFolder}/node_modules/jest/bin/jest"
  }
}

Notice that the name is important here, if it does not start with vscode-jest-tests.v2 the default config will be generated and used implicitly.

@davidcoleman007
Copy link

@olifer this does not solve the problem.

I have also followed these steps, but they were not necessary in the first place as the command line is being generated fine without customizing the config.

the issue is that the generated command line is missing quotes around the "npm.cmd" path.

Please see here:
image

@davidcoleman007
Copy link

davidcoleman007 commented Jul 24, 2024

@connectdotz
here is a paste of my debug config (some private info has been masked)

    {
      "type": "node",
      "name": "vscode-jest-tests.v2.{reponame}",
      "request": "launch",
      "args": [
        "test",
        "--",
        "--runInBand",
        "--watchAll=false",
        "--testNamePattern",
        "${jest.testNamePattern}",
        "--runTestsByPath",
        "${jest.testFile}"
      ],
      "cwd": "c:\\code\\{reponame}",
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "disableOptimisticBPs": true,
      "runtimeExecutable": "npm"
    }

jest is generating a command line seen here: (again private info masked)

/usr/bin/env 'NODE_OPTIONS= --require "c:/Users/*****/AppData/Local/Programs/Microsoft VS Code/resources/app/extensions/ms-vscode.js-debug/src/bootloader.js"  --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS=:::{"inspectorIpc":"\\\\.\\pipe\\node-cdp.34560-666e75fc-19.sock","deferredMode":false,"waitForDebugger":"","execPath":"C:\\Program Files\\nodejs\\node.exe","onlyEntrypoint":false,"autoAttachMode":"always","fileCallback":"C:\\Users\\*****\\AppData\\Local\\Temp\\node-debug-callback-72e6e64a92552add"}' C:\\Program\ Files\\nodejs\\npm.cmd test -- --runInBand --watchAll=false --testNamePattern VerbCompressPdfConfig\ tests\ expects\ the\ right\ bootstrap\ properties\ to\ be\ loaded\$ --runTestsByPath C:\\code\\*****\\src\\bootstrapConfig\\CompressPDF\\VerbCompressPdfConfig.spec.js 

the correct command line would be:

/usr/bin/env 'NODE_OPTIONS= --require "c:/Users/*****/AppData/Local/Programs/Microsoft VS Code/resources/app/extensions/ms-vscode.js-debug/src/bootloader.js"  --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS=:::{"inspectorIpc":"\\\\.\\pipe\\node-cdp.34560-666e75fc-19.sock","deferredMode":false,"waitForDebugger":"","execPath":"C:\\Program Files\\nodejs\\node.exe","onlyEntrypoint":false,"autoAttachMode":"always","fileCallback":"C:\\Users\\*****\\AppData\\Local\\Temp\\node-debug-callback-72e6e64a92552add"}' "C:\\Program\ Files\\nodejs\\npm.cmd" test -- --runInBand --watchAll=false --testNamePattern VerbCompressPdfConfig\ tests\ expects\ the\ right\ bootstrap\ properties\ to\ be\ loaded\$ --runTestsByPath C:\\code\\*****\\src\\bootstrapConfig\\CompressPDF\\VerbCompressPdfConfig.spec.js 

Please notice the additional quotes around the "C:\\Program\ Files\\nodejs\\npm.cmd"

@davidcoleman007
Copy link

davidcoleman007 commented Jul 24, 2024

also, i would ask, how can we replace the usage of "{npm cmd line}" test with something more generic such as npx jest? Not all projects have a "test" script that is suitable for this purpose. In our case, for many (maybe wrong but beyond my power to change) reasons, our "test" script also runs linting and a number of other unnecessary (in the context of "testing") steps, which make it not suitable for use as the debug test runner.

It is not scalable to ask developers to locally change their test script to be pure test scripts just to use this tool. This tool should be configurable to run tests however the project requires.

The previous iteration of this project "Jest Test Explorer UI" had no such problems with our projects. I will have to continue to use it until these issues are fixed.

@davidcoleman007
Copy link

@connectdotz Just following up on this. Is there further information that can help us find a resolution?

@connectdotz
Copy link
Collaborator

connectdotz commented Aug 2, 2024

@davidcoleman007 I have a few suggestions:

  1. If you are on Windows, try using the "windows" config as @olifer mentioned.
  2. You don't need to use npm test. If you want to run npx jest, you can change the config accordingly.

For example, you can try something like this:

{
  "type": "node",
  "name": "vscode-jest-tests.v2.{reponame}",
  "request": "launch",
  "args": [
    "jest",  // <= changed
    "--",
    "--runInBand",
    "--watchAll=false",
    "--testNamePattern",
    "${jest.testNamePattern}",
    "--runTestsByPath",
    "${jest.testFile}"
  ],
  "cwd": "c:\\code\\{reponame}",
  "console": "integratedTerminal",
  "internalConsoleOptions": "neverOpen",
  "disableOptimisticBPs": true,
  "windows": {
    "runtimeExecutable": "npx" // <= changed
  }
}

As you can see, you have full control over what command the debugger should launch with. The extension only substitutes the variables in the config, such as ${jest.testNamePattern}; it doesn't impose any additional constraints. Feel free to experiment (like adding quotes or the full path of the program if VSCode didn't spawn the right command line), and let us know if you have any additional questions.

@stefanone91
Copy link

I've resolved simply by calling directly jest from node modules instead of passing through npm.
This is my launch.json config:

{
            "type": "node",
            "name": "vscode-jest-tests.v2.{PROJECT_NAME}", // Adapt this
            "request": "launch",
            "args": [ 
                // Removed first two params from the command
                "--runInBand",
                "--watchAll=false",
                "--testNamePattern",
                "${jest.testNamePattern}",
                "--runTestsByPath",
                "${jest.testFile}"
            ],
            "cwd": "${workspaceRoot}",
            "console": "integratedTerminal",
            "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/jest", // Changed this
}

@connectdotz
Copy link
Collaborator

Great! Yes, the debug configuration in VSCode is quite flexible—kudos to the team for that. I encourage everyone to experiment with it; you should be able to find a solution that works for your project.

Since no further action is required from the extension, I’ll go ahead and close this issue.

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

5 participants