You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We've encountered a problem in our pnpm monorepo when __mocks__ folder module automocking doen't work for packages that are imported from pnpm workspace rather than npm registry.
When we try to import a package from NPM registry as usual, it works fine, but importing the same package as "workspace:*" in package.json leads to vitest silently ignoring __mocks__ folder based mocks.
What you can see there is a tiny pnpm monorepo with three packages: package_one - a thing we're importing and mocking, and package_two and package_three which both import first package and mock it using __mocks__ folder.
Package two imports first package from pnpm workspace, and package three imports first package from NPM registry.
Despite latter packages do the very same thing in an exactly same setup, tests for package_two are failing because mock wasn't successful.
I'm not sure if this is expected to work out-of-the-box since workspace:* uses a symlink which points to original source path. Currently __mocks__ feature expects the actual path to live inside /node_modules/ convention.
@hi-ogawa Thank you for the hint, this works like a charm indeed!
In my humble opinion this behaviour is a bit confusing for end users, so if there's no easy way to fix this, maybe it worth mentioning this in docs as a caveat?
TBH I've spent ridiculous amount of time just to figure out it wasn't me doing something wrong, but vitest just silently ignored my mocks. It would save vitest users from some additional headache if this implementation detail would be mentioned somewhere.
Describe the bug
We've encountered a problem in our pnpm monorepo when
__mocks__
folder module automocking doen't work for packages that are imported from pnpm workspace rather than npm registry.When we try to import a package from NPM registry as usual, it works fine, but importing the same package as "workspace:*" in package.json leads to vitest silently ignoring
__mocks__
folder based mocks.Reproduction
StackBlitz: https://stackblitz.com/~/github.com/Koka/vitest_pnpm_problem
What you can see there is a tiny pnpm monorepo with three packages:
package_one
- a thing we're importing and mocking, andpackage_two
andpackage_three
which both import first package and mock it using__mocks__
folder.Package two imports first package from pnpm workspace, and package three imports first package from NPM registry.
Despite latter packages do the very same thing in an exactly same setup, tests for
package_two
are failing because mock wasn't successful.System Info
Used Package Manager
pnpm
Validations
The text was updated successfully, but these errors were encountered: