Vscode-jest: Support pnp zero-config

Created on 4 Jun 2020  ·  6Comments  ·  Source: jest-community/vscode-jest

Environment

  1. node -v: 12.18.0
  2. npm -v: 6.14.4
  3. yarn why jest
❯ yarn why jest
└─ @yarnpkg/monorepo@workspace:.
   └─ jest@npm:24.9.0 (via npm:^24.9.0)
  1. your vscode-jest settings if customized:

    • jest.pathToJest? None
    • jest.pathToConfig? None
    • anything else that you think might be relevant? Yarn v2 PNP
  2. Operating system: macOS 10.15.5

Prerequisite

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

Steps to Reproduce

Clone the project https://github.com/yarnpkg/berry and open in vscode with vscode-jest extension installed. Go to Jest output.

[fill]

Relevant Debug Info

The getLocalPathForExecutable logic here does not consider yarn pnp configurations. The workaround is to manually specify the pathToJest but the correct behavior would be to properly resolve pnp binary. See https://github.com/yarnpkg/berry/pull/1440 for more info.

[fill]

Expected Behavior

vscode-jest should detect pnp and run yarn run jest instead of looking for a binary in node_modules

Actual Behavior

/bin/sh: jest: command not found

Finished running all tests. Starting watch mode.
/bin/sh: jest: command not found

/bin/sh: jest: command not found

/bin/sh: jest: command not found

Starting Jest in Watch mode failed too many times and has been stopped.
 see troubleshooting: https://github.com/jest-community/vscode-jest/blob/master/README.md#troubleshooting
/bin/sh: jest: command not found

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

this settings.json works.

{
  "jest.pathToJest": "yarn jest"
}

All 6 comments

this settings.json works.

{
  "jest.pathToJest": "yarn jest"
}

@esetnik I'm having a look at this issue now. Do you have a good understanding of what you can look at in a repository to check that it is a Yarn PnP-based codebase? What I need to do is understand what conditions need to be met so that I can set the pathToJest command.

Things I can check:

  • presence of .pnp.js file in repo root.
  • presence of yarn.lock file (and/or absence of package.lock file)
  • specific keys in the package.json file (e.g. workspaces)
  • specific packages (e.g. @yarnpkg/pnpify)

What I'm looking for is a fairly reliable way to identify if it is a pnp repo.

@rossknudsen I think it should first try node_modules/.bin, if fail check .pnp.js exists.

Adding "jest.pathToJest": "yarn jest" appears to fix running tests but not debugging them.

UPDATE:
Just found https://github.com/jest-community/vscode-jest/issues/618 which addresses this problem specifically.

This launch.json appears to work:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "name": "vscode-jest-tests",
            "request": "launch",
            "runtimeExecutable": "yarn",
            "runtimeArgs": ["run", "--inspect-brk", "jest", "--runInBand"],
            "cwd": "${workspaceFolder}",
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen",
            "disableOptimisticBPs": true
        }
    ]
}

@OzzieOrca great to hear you got it resolved 👍 and sorry you had to struggle through it in the dark...

We just released a "Setup Wizard" in v4.0.0-alpha.5 that hopefully will streamline this process and spare the frustration. Please feel free to give it a try and let us know how it goes.

@esetnik instead of auto-detecting pnp, we now offered a "Setup Wizard" to help people customize their environment with simple steps. Would love to have you try it to see if it can setup the pnp env correctly: v4.0.0-alpha.5

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FDiskas picture FDiskas  ·  3Comments

exapsy picture exapsy  ·  3Comments

orta picture orta  ·  5Comments

ryanlittle picture ryanlittle  ·  3Comments

jpokrzyk picture jpokrzyk  ·  5Comments