Vscode-jest: pathToJest is ignored when using inline "Debug" button

Created on 25 Aug 2020  路  9Comments  路  Source: jest-community/vscode-jest

Environment

  1. node -v: 12.18.3
  2. npm -v: 6.14.6
  3. npm ls jest or npm ls react-scripts (if you haven鈥檛 ejected): 24.9.0
  4. your vscode-jest settings if customized:

    • jest.pathToJest? /usr/local/opt/custom-build-system/libexec/node_modules/.bin/jest
    • jest.pathToConfig? jest.config.json
    • anything else that you think might be relevant? [fill]
  5. Operating system: macOS 10.15.5

Prerequisite

  • are you able to run jest test from command line? yes
  • how do you run your tests from command line? (for example: npm run test or node_modules/.bin/jest) yarn test:jest

Steps to Reproduce

  1. Set a custom jest.pathToJest in settings
  2. Hit debug button on an individual test
  3. Observe output of debug command

Relevant Debug Info

N/A

Expected Behavior

When I click the "Debug" button that appears above each test, the command should use the jest path I specified in pathToJest

Actual Behavior

When I click the "Debug" button that appears above each test, the command uses my local jest installation instead of the one I defined in pathToJest

Output after clicking debug:

env CI=vscode-jest-tests 'NODE_OPTIONS=--require "/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/ms-vscode.js-debug/src/bootloader.bundle.js" ' 'VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"/var/folders/37/dk8xxg5j0vv7n0prnt8n258w0000gn/T/node-cdp.67736-1.sock","deferredMode":false,"waitForDebugger":"","execPath":"/usr/local/bin/node","onlyEntrypoint":false,"fileCallback":"/var/folders/37/dk8xxg5j0vv7n0prnt8n258w0000gn/T/node-debug-callback-1356291d1dfcd36e"}' /usr/local/bin/node ./node_modules/jest/bin/jest --runInBand FileName.test.jsx --testNamePattern "should equal true"
Debugger listening on ws://127.0.0.1:53279/95f59763-2d8d-42cf-b07f-4fb490b0eab4

Notice how it's pointing to ./node_modules/jest/bin/jest instead of the custom path.


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

Most helpful comment

I can verify this behaviour, pathToJest is respected for other functionality but not when using the inline "Debug" button - the extension attempts to find jest relative to the current workspace instead.

All 9 comments

I can verify this behaviour, pathToJest is respected for other functionality but not when using the inline "Debug" button - the extension attempts to find jest relative to the current workspace instead.

Yes, we have gone through some discussions about this... vscode has its own debug mechanism, i.e. it launched the program (jest) via launch config instead of letting plugin launch jest (with the custom jest.pathToJest for example) like during non-debug mode. While we could try to parse the jest.pathToConfig, it could be error-prone; in addition, many projects might have different jest cli for debugging vs. regular run anyway... So we provided some debug config templates for people to customize for their env instead...

I agree this is not very intuitive, we plan to address it by providing a "setup wizard" (#590) to walk people through these settings when needed, suggestions/comments are welcome.

Hi @connectdotz, I've probably missed something obvious, but can you please elaborate on

So we provided some debug config templates for people to customize for their env instead...

Where can I find those debug config templates? I assume they are supposed to go in launch.json ? My use case is I'm using yarn 2 and the extension can't find my jest installation, so I need to specify that it runs yarn jest instead.

I have the same use case as @aholachek where I need to specify running yarn jest with difference being im inside a lerna managed package.

I'm also being tripped up by this issue, or some very similar issue. I have a multi-root workspace with 30+ packages, new packages are being added all the time. We do not use a local node_modules per package, so jest is not installed per-package. My system-wide path to jest is /usr/local/bin/jest.

If I create a launch.json inside a package, and set "program": "/usr/local/bin/jest", I can debug tests in that package by clicking the inline "Debug" button. But I don't want to do this for every package. I would prefer to have one global launch configuration in my multi-root workspace (eg. in workspace file), and clicking the inline "Debug" button would start that configuration. Then I could override that with a local launch.json only when needed.

But when I add the same launch configuration to my workspace file, it seems like vscode-jest does not use it. Instead it creates the default config, which tries to run jest from a local node_modules './node_modules/jest/bin/jest'.

How can I make vscode-jest find and launch a global "vscode-jest-tests" config when clicking the inline "Debug" button, regardless of which package I start it in?

  1. open your launch.json file.
  2. vscode will show an Add Configuration... button at the lower right corner, click on it
  3. it will show a list of "templates" you can choose from. Look for the template starts with Jest: such as Jest: Default jest configuration, Jest: create-react-app
  4. your selected template will be added to the launch.json under the name vscode-jest-tests. You can customize it as you wish, for example, change runtimeExecutable to run npm script: see here

This extension will look for vscode-jest-tests debug config and append the test file/name when you click on the debug code-lense.

We probably should have documented this better... Let me know if you still have any problems...

Thanks for offering your help!
I followed your steps, to create a global launch configuration in my root launch.json (the one at the same level as node_modules) and also in the "launch" section of my .code-workspace file. But when I click the Debug button, vscode-jest does not attempt to use either global launch configuration, it tries to launch '/node_modules/jest/bin/jest' in the package that contains the test file, which I think is the default path hard-coded in vscode-jest. This fails because there is no node_modules folder in the package.

If I add a launch.json that is local to the package containing the test file, vscode-jest will try to launch whatever is specified in the local configuration. But as I say, I would prefer not to define a local launch configuration in every package.

Any reason why jest.rootPath is also being ignored ?

hi, this issue has been addressed with the "Setup Wizard" in the latest v4.0.0-alpha.5, where it will take rootPath, commandLine settings when generating the debug config. Feel free to give it a try and let us know.

Was this page helpful?
0 / 5 - 0 ratings