vitest-dev / vitest-dev/vitest

Module __mocks__ in pnpm monorepo doesn't work for workspace packages

Open
#7,708 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

p3-minor-bug
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 22h
Merged PRs (30d)
94

Description

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, 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.

System Info
System:
    OS: macOS 15.3.1
    CPU: (12) arm64 Apple M3 Pro
    Memory: 4.77 GB / 36.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
    npm: 10.2.3 - ~/.nvm/versions/node/v20.10.0/bin/npm
    pnpm: 8.6.12 - ~/Library/pnpm/pnpm
  Browsers:
    Safari: 18.3
Used Package Manager

pnpm

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the StackBlitz reproduction and compare package_two, which imports the workspace package and has failing tests, with package_three, which imports the registry package and passes. Trace how Vitest discovers mocks for each package setup; done means workspace:* imports honor the same folder-based automocking behavior as registry imports.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.