vitest
vitest copied to clipboard
utils.ensurePackageInstalled is not compatible with yarn PnP
Describe the bug
Trying to run vitest in a package of a yarn monorepo will cause the following error:
MISSING DEP Can not find dependency 'vite'
✖ Do you want to install vite? … no
I already have vite installed and apparently the issue is caused by this line: https://github.com/vitest-dev/vitest/blob/f557565626e74e3345b417345bd53213c240ecf8/packages/vitest/src/utils/index.ts#L42
The local-pkg
is not compatible with yarn PnP, as confirmed by this issue.
As far as I understand it, it should be enough to call isPackageExists
with a second options parameters like this:
isPackageExists(dependency, { paths: [ process.cwd() ] })
I already tested this solution locally with a fork of local-pkg
and it works.
Reproduction
The issue is already confirmed.
System Info
System:
OS: Linux 5.10 Ubuntu 20.04.3 LTS (Focal Fossa)
CPU: x64 Intel [redacted]
Memory: 2.72 GB / 7.63 GB
Container: Yes
Shell: 5.0.17 - /bin/bash
Binaries:
Node: 16.13.2 - /usr/bin/node
Yarn: 3.1.1 - /usr/bin/yarn
npm: 8.1.2 - /usr/bin/npm
Used Package Manager
yarn
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.
I additionally had the following problem which I think stems from vitest requiring happy-dom through local-pkg:
Error: local-pkg tried to access happy-dom, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.
Required package: happy-dom (via "happy-dom/package.json")
https://github.com/vitest-dev/vitest/blob/f5189fd3e3b3e361948ea30c6e2d5732fe6ff6a0/packages/vitest/src/integrations/env/happy-dom.ts#L8
After patching vitest with @pietrovismara's fix I had to add the following to yarnrc.yml
though it feels very wrong since it feels like vitest should need to be specified and not local-pkg for extension. However fixed the issue.
"local-pkg@*": # This is required for vitest as it accesses happy-dom through local-pkg on its behalf
dependencies:
"happy-dom": "*"
we are looking to migrate from jest to vitest but we have the same issue with a yarn2 monorepo
For anyone trying to use Vitest with pnpm in a monorepo, the following .npmrc
config seems to work as a workaround:
hoist-pattern[]="vite"
The issue should still be fixed, but as a temporary solution you can add this to .yarnrc.yml
:
pnpMode: "loose"
packageExtensions:
"vite@*":
dependencies:
"supports-color": "*"
"debug@*":
dependencies:
"supports-color": "*"
Also happens with pnpm and hoist=false
in .npmrc
.
Error looks like:
Error: Cannot find package 'jsdom' imported from [REDACTED]/node_modules/.pnpm/[email protected]/node_modules/local-pkg/index.mjs