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

Snapshot Error with Yarn PnP in Workspace Using setupFiles #6887

Open
6 tasks done
custardcream98 opened this issue Nov 9, 2024 · 4 comments
Open
6 tasks done

Snapshot Error with Yarn PnP in Workspace Using setupFiles #6887

custardcream98 opened this issue Nov 9, 2024 · 4 comments

Comments

@custardcream98
Copy link

custardcream98 commented Nov 9, 2024

Describe the bug

In the following reproduction, running this script works without any errors:

yarn test

However, the following command throws an unexpected error:

yarn workspace sample test
TypeError: Cannot read properties of undefined (reading 'match')
 ❯ snap.test.js:2:20
      1| it("should do inline snapshot test", () => {
      2|   expect("sample").toMatchInlineSnapshot(`"sample"`);
       |                    ^
      3| });
      4| 

If I comment out packages/sample/vitest.setup.js, then yarn workspace sample test runs without errors.

Additional Context

Upon further investigation, I found similar issues mentioned in #3426, but this problem occurs even though all peer dependencies of Vitest are the same in both the root and the workspace.

Here's the result of yarn why vitest (you can try this yourself on CodeSandbox):

➜  workspace git:(master) ✗ yarn why vitest
├─ sample@workspace:packages/sample
│  └─ vitest@npm:2.1.4 [e0fc9] (via npm:^2.1.4 [e0fc9])
│
└─ yarn-issue@workspace:.
   └─ vitest@npm:2.1.4 [71bfa] (via npm:^2.1.4 [71bfa])

It seems like two different virtual folders are created for Vitest, even though all peer dependencies are installed identically. I suspect the issue might be caused by @vitest/ui, which lists vitest as a peer dependency, and vice versa (vitest also has @vitest/ui as a peer dependency).

If I remove @vitest/ui from both the root and the workspace, then the virtual folders for vitest are unified, and the issue is resolved.

In conclusion, this issue might be fixed if the common logic shared between vitest and @vitest/ui is extracted into a separate independent package, which both vitest and @vitest/ui can depend on.

I’m still not sure why the issue is resolved when I comment out the code specified in setupFiles. By tracing the error stack, I found that when setupFiles is used, the snapshot client instance is created using the root vitest, but during the actual test, it attempts to access the snapshot client instance from the workspace’s vitest (just like described in #3426).

Reproduction

(had to use codesandbox instead to use yarn berry)

https://codesandbox.io/p/devbox/yarn-pnp-issue-4tx7yh

System Info

System:
    OS: Linux 6.1 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
    CPU: (2) x64 AMD EPYC
    Memory: 1.71 GB / 4.01 GB
    Container: Yes
    Shell: Unknown
  Binaries:
    Node: 20.9.0 - /usr/local/bin/node
    Yarn: 4.5.0 - /usr/local/bin/yarn
    npm: 9.8.1 - /usr/local/bin/npm
    pnpm: 8.10.2 - /usr/local/share/npm-global/bin/pnpm

Used Package Manager

yarn

Validations

@hi-ogawa
Copy link
Contributor

I'm not familiar with pnp, but wouldn't @testing-library/jest-dom be also to blame for not having optional peerDependencies: "vitest"? https://github.com/testing-library/jest-dom/blob/918b6fbcde10d4409ee8f05c6e4eecbe96a72b7a/vitest.js#L1
I thought yarn pnp wouldn't allow picking up such ghost dependencies.

Vitest having two way peer dep is not a good thing, but we haven't figured out how to avoid that. Btw, I think we also have an issue with @vitest/browser too.

@custardcream98
Copy link
Author

@hi-ogawa

Thank you for checking!
Yes, @testing-library/jest-dom has ghost dependency issues. Upon checking, it seems like it was initially declared as a peer dependency but was later removed (I'm not entirely sure why this decision was made).

However, Yarn provides a way to manually fix ghost dependencies, and I tried that, but the problem persists.

CodeSandbox

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Nov 14, 2024

Hmm, interesting, I thought they had optional peer dep at some point. FWIW, downgrading 6.4.7 seems to work, which is right before they remove peer dep.

@custardcream98
Copy link
Author

@hi-ogawa
yarn workspace sample test works fine, but
yarn test --project sample or justyarn test both still result in the same error 😢

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