Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
6 tasks done
Koka opened this issue Mar 20, 2025 · 2 comments
Open
6 tasks done

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

Koka opened this issue Mar 20, 2025 · 2 comments

Comments

@Koka
Copy link

Koka commented Mar 20, 2025

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

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Mar 21, 2025

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.

You may try tweaking moduleDirectories to treat @koka58/package_one like an external package. Here is an updated repro https://stackblitz.com/edit/github-ct5cs7z2?file=packages%2Fpackage_two%2Fvitest.config.ts

import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    deps: {
      moduleDirectories: ['/node_modules/', '/packages/package_one/'],
    },
  },
});

@Koka
Copy link
Author

Koka commented Mar 21, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants