jest-community / vscode-jest

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

`command not found: jest` #899

Closed taschetto closed 1 year ago

taschetto commented 2 years ago

Environment

  1. vscode-jest version: 4.6.0
  2. node -v: 14.20
  3. npm -v or yarn --version: 8.18.0
  4. npm ls jest or npm ls react-scripts (if you haven’t ejected):
backend@0.1.0 /home/taschetto/projects/org/backend
├─┬ jest-watch-typeahead@1.1.0
│ └── jest@27.5.1 deduped
├── jest@27.5.1
└─┬ ts-jest@27.1.5
  └── jest@27.5.1 deduped
  1. your vscode-jest settings if customized:
  "jest.autoRun": "off",
  "jest.jestCommandLine": "node_modules/.bin/jest", // same behavior when `undefined` or just `npm run tests`
  "jest.shell": "/bin/zsh" // same behavior when `undefined`
  1. Operating system: Ubuntu 22.04 LTS

Prerequisite

Steps to Reproduce

We have a monorepo with the following structure:

.
├── jest.config.js
├── node_modules
├── package.json
├── package-lock.json
├── packages
└── test

The extension works fine for projects inside packages/. But we also have a project at the root level . (CDK project, tests inside ./tests, can run tests in the terminal with npm run tests)

Whenever I try to run tests via the Text Explorer for that particular project, I get:

all-tests-56 is scheduled
zsh:1: command not found: jest

Expected Behavior

It should run the tests.

Actual Behavior

all-tests-56 is scheduled
zsh:1: command not found: jest
taschetto commented 2 years ago

Installing jest as a global package seems to solve the issue, but I don't believe it's the best solution.

connectdotz commented 2 years ago

This sounds like a monorepo project set-up issue. Please take a look at the troubleshooting/jest-failed-to-run, particularly the monorepo projects section. Let me know if it helps.

gedclack commented 2 years ago

This also happen to me after updated my VSCode to the newest (Version: 1.71.1 (Universal)), my Test Editor also does not work anymore.

*I am using multi-root workspace.

Before updating my VSCode, all works fine, even the Test Editor shows correctly.

taschetto commented 2 years ago

This sounds like a monorepo project set-up issue. Please take a look at the troubleshooting/jest-failed-to-run, particularly the monorepo projects section. Let me know if it helps.

The setup is correct.

connectdotz commented 2 years ago

@taschetto, I can take a deeper look if you can put together a sample repo.

@gedclack, does it return to normal if you restart code? I have seen the restart from the new vscode version sometimes caused shell env not to be initialized properly...

gedclack commented 2 years ago

@connectdotz Yes! I am on MacOS, I need to quit VSCode and open it again. It does not work if I only Close the editor window.

javifm86 commented 2 years ago

Same here, but extension is not working, although I quit VS Code. I have a monorepo, everything was working fine (I configured some months ago).

I started getting this message around the end of August. Not only that, but I guess something has changed in VS Code, when I shut down my macOS (keeping my applications open for the next session), I get a warning message about some environment variable could not be initialized.

image

The problem is related to NVM (which I use), but if I restart VS Code that warning disappears and shell environment are loaded successfully.

But this is still there:

image

Just in case it can be related in any way. Maybe this information can help you, maybe not.

connectdotz commented 2 years ago

@javifm86 are you able to examine the process env of the spawned process? exit code 127 means "command not found", which usually indicates process.env is not fully set up as expected. See self-diagnosis for how to check process.env.

It is interesting that it was working fine until Aug...🧐

javifm86 commented 2 years ago

Sure. Today the frontend part is working fine (I would swear I checked it on Fridays, and it was not working), but the backend part is not working. Here you have the relevant information from DevTools for backend:

[Extension Host] [backend/ProcessSessionManager] scheduling jest process: list-test-files
{
    "ELECTRON_RUN_AS_NODE": "1",
    "USER": "ambes000083",
    "MallocNanoZone": "0",
    "__CFBundleIdentifier": "com.microsoft.VSCode",
    "COMMAND_MODE": "unix2003",
    "LOGNAME": "ambes000083",
    "PATH": "/Users/ambes000083/.nvm/versions/node/v16.13.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin",
    "SSH_AUTH_SOCK": "/private/tmp/com.apple.launchd.H7HAitRYTk/Listeners",
    "SHELL": "/bin/zsh",
    "HOME": "/Users/ambes000083",
    "__CF_USER_TEXT_ENCODING": "0x1F5:0x0:0x0",
    "TMPDIR": "/var/folders/0g/2kdy6xf13ddcgg5n1bxkdmyc0000gn/T/",
    "XPC_SERVICE_NAME": "application.com.microsoft.VSCode.28571057.28571063",
    "XPC_FLAGS": "0x0",
    "ORIGINAL_XDG_CURRENT_DESKTOP": "undefined",
    "VSCODE_CWD": "/",
    "VSCODE_NLS_CONFIG": "{\"locale\":\"en\",\"availableLanguages\":{},\"_languagePackSupport\":true}",
    "VSCODE_CODE_CACHE_PATH": "/Users/ambes000083/Library/Application Support/Code/CachedData/74b1f979648cc44d385a2286793c226e611f59e7",
    "VSCODE_IPC_HOOK": "/Users/ambes000083/Library/Application Support/Code/1.71.2-main.sock",
    "VSCODE_PID": "3100",
    "SHLVL": "0",
    "PWD": "/",
    "OLDPWD": "/",
    "ZSH": "/Users/ambes000083/.oh-my-zsh",
    "PAGER": "less",
    "LESS": "-R",
    "LSCOLORS": "Gxfxcxdxbxegedabagacad",
    "NVM_DIR": "/Users/ambes000083/.nvm",
    "NVM_CD_FLAGS": "-q",
    "NVM_BIN": "/Users/ambes000083/.nvm/versions/node/v16.13.1/bin",
    "NVM_INC": "/Users/ambes000083/.nvm/versions/node/v16.13.1/include/node",
    "_": "/Applications/Visual Studio Code.app/Contents/MacOS/Electron",
    "VSCODE_AMD_ENTRYPOINT": "vs/workbench/api/node/extensionHostProcess",
    "VSCODE_HANDLES_UNCAUGHT_ERRORS": "true",
    "APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL": "true"
}
[Extension Host] [backend/ListTestFileListener] not-test onExecutableStdErr: /bin/sh: jest: command not found
notificationsAlerts.ts:42 not-test onProcessExit: process exit with code=127, signal=undefined
connectdotz commented 1 year ago

It seems vscode didn't initialize the process env completely. For people who frequently bumped into this issue, you can try switching to login shell added in v5 (still pre-release), and let us know if it resolved your issue.

if this works out well, we could even consider making the login shell the default. Then we don't need to rely on vscode's start-up initialization, which looked quite unstable...

This is a frequent complaint. We should either address the root cause or otherwise provide a self-help tool to diagnose and suggest possible solutions accordingly. Asking people to read documents just didn't seem very efficient. :thinking:

connectdotz commented 1 year ago

@taschetto I looked at your original issue content more closely today, and noticed your comment:

 "jest.jestCommandLine": "node_modules/.bin/jest", // same behavior when `undefined` or just `npm run tests`

Not sure if it is a typo or a real problem, but you should set "jest.jestCommandLine": "npm run test --" to match how you run tests in the terminal. Your issue appeared to be different than the others in the thread, which is intermittent and most likely related to process.env.

Anyway, let us know if that resolves your issue.

javifm86 commented 1 year ago

This is a frequent complaint. We should eithe

Last Fridays it worked again, but today it is not working, therefore the initialization for VSCode is causing the problem. I will try the login shell.

Thank you for your time.

connectdotz commented 1 year ago

for people who landed here, we believe most of the issues have been addressed in v5 pre-release, please feel free to give it a spin and let us know otherwise.

asifrahmanvm commented 1 year ago

login shell

Using the pre-release version v5.2 but still same problem. Sometimes it works other times it doesn't.

connectdotz commented 1 year ago

@asifrahmanvm, what is the error message when it fails?

clxrityy commented 6 months ago

I was encountering this error when trying to follow someone else's code, this is the example repo

Every time I ran pnpm i and/or pnpm update I was also getting a separate error.

By doing sudo pnpm i then sudo pnpm update, my pnpm test (jest watchAll --verbose), worked fine.

In short, I had to make sure the package was properly installed.