Contents

Jest 无法找到yarn等命令的解决方案

Contents

Jest 无法找到 yarn 等命令的解决方案

如果你不是从你的主要终端环境开启的 vscode,可能就会遇到这种 xxx: command not found 的错误:

/bin/bash: line 1: yarn: command not found

这是由于 vscode-jest 插件执行 command 时使用的是当前 process.env 的环境:

参考链接

the env and ENOENT issues have come up quite a bit, let’s see if we can nail them:

vscode-jest spawn a child_process to run jest (or custom jest.pathToJest) on user’s behave. The spawned process’s env is based on process.env.

You can see it might not use your favor shell’s env but the one specified in environ(7). You can try man environ to see how you can view and > customize the env on your platform. Let me know how it goes.

这导致它的执行环境与你默认的终端环境不同,那么解决问题的方案就是解决它的终端环境的解析问题,这可以通过 jest.jestCommandLine 配置实现,参考链接

配置你的 .vscode/settings.json 文件

{
  "jest.jestCommandLine": "source ~/.zshrc &&  yarn test"
}

命令 jest.jestCommandLine 的介绍:

参考链接

This should be the command users used to kick off the jest tests in the terminal. However, since the extension will append additional options at run time, please make sure the command line can pass along these options, which usually just means if you uses npm, add an additional “–” at the end (e.g. “npm run test –") if you haven’t already in your script. It is recommended not to add the following options as they are managed by the extension: –watch, –watchAll, –coverage

参考链接

jest-community