jest-community / vscode-jest

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

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

Closed kruncher closed 3 months ago

kruncher commented 5 months ago

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:

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

Prerequisite

connectdotz commented 5 months ago

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 commented 5 months ago

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 commented 4 months ago

@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 commented 4 months ago

@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 commented 4 months ago

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 commented 3 months ago

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

connectdotz commented 3 months ago

@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 commented 3 months ago

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 commented 3 months ago

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.