vitest: linked dependency causes tests to fail

Describe the bug

Thanks so much for creating such wonderful tooling πŸ˜ƒ

I believe I have found a bug and hope I can describe it well enough here:

  • a library project that has @mui/base as a dependency and exports files using it
  • a test project that uses the library and has tests executed via vitest
  • hook the library into the test project via a file: resolution and the tests pass βœ…
  • hook the library into the test project via a link: resolution and the tests fail with this error message ❌:
yarn run v1.22.19
$ vitest run

 RUN  v0.34.3 /Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project

stderr | src/Test.test.jsx > renders a button
Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
Error: Uncaught [TypeError: Cannot read properties of null (reading 'useRef')]
    at reportException (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:66:24)
    at innerInvokeEventListeners (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:353:9)
    at invokeEventListeners (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3)
    at HTMLUnknownElementImpl._dispatch (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9)
    at HTMLUnknownElementImpl.dispatchEvent (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17)
    at HTMLUnknownElement.dispatchEvent (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:241:34)
    at Object.invokeGuardedCallbackDev (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/react-dom/cjs/react-dom.development.js:4213:16)
    at invokeGuardedCallback (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/react-dom/cjs/react-dom.development.js:4277:31)
    at beginWork$1 (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/react-dom/cjs/react-dom.development.js:27451:7)
    at performUnitOfWork (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/react-dom/cjs/react-dom.development.js:26560:12) TypeError: Cannot read properties of null (reading 'useRef')
    at Object.useRef (/Users/aokeeffe/Developer/vitest-link-reproduction/node_modules/react/cjs/react.development.js:1630:21)
    at Button (/Users/aokeeffe/Developer/vitest-link-reproduction/node_modules/@mui/base/node/Button/Button.js:54:27)
    at renderWithHooks (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/react-dom/cjs/react-dom.development.js:16305:18)
    at updateForwardRef (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/react-dom/cjs/react-dom.development.js:19226:20)
    at beginWork (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/react-dom/cjs/react-dom.development.js:21636:16)
    at HTMLUnknownElement.callCallback (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/react-dom/cjs/react-dom.development.js:4164:14)
    at HTMLUnknownElement.callTheUserObjectsOperation (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30)
    at innerInvokeEventListeners (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25)
    at invokeEventListeners (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3)
    at HTMLUnknownElementImpl._dispatch (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9)
Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
Error: Uncaught [TypeError: Cannot read properties of null (reading 'useRef')]
    at reportException (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:66:24)
    at innerInvokeEventListeners (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:353:9)
    at invokeEventListeners (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3)
    at HTMLUnknownElementImpl._dispatch (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9)
    at HTMLUnknownElementImpl.dispatchEvent (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17)
    at HTMLUnknownElement.dispatchEvent (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:241:34)
    at Object.invokeGuardedCallbackDev (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/react-dom/cjs/react-dom.development.js:4213:16)
    at invokeGuardedCallback (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/react-dom/cjs/react-dom.development.js:4277:31)
    at beginWork$1 (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/react-dom/cjs/react-dom.development.js:27451:7)
    at performUnitOfWork (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/react-dom/cjs/react-dom.development.js:26560:12) TypeError: Cannot read properties of null (reading 'useRef')
    at Object.useRef (/Users/aokeeffe/Developer/vitest-link-reproduction/node_modules/react/cjs/react.development.js:1630:21)
    at Button (/Users/aokeeffe/Developer/vitest-link-reproduction/node_modules/@mui/base/node/Button/Button.js:54:27)
    at renderWithHooks (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/react-dom/cjs/react-dom.development.js:16305:18)
    at updateForwardRef (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/react-dom/cjs/react-dom.development.js:19226:20)
    at beginWork (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/react-dom/cjs/react-dom.development.js:21636:16)
    at HTMLUnknownElement.callCallback (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/react-dom/cjs/react-dom.development.js:4164:14)
    at HTMLUnknownElement.callTheUserObjectsOperation (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30)
    at innerInvokeEventListeners (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25)
    at invokeEventListeners (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3)
    at HTMLUnknownElementImpl._dispatch (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/vite-project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9)
The above error occurred in the <ForwardRef(Button)> component:

    at Button (/Users/aokeeffe/Developer/vitest-link-reproduction/node_modules/@mui/base/node/Button/Button.js:47:7)
    at p (/Users/aokeeffe/Developer/vitest-link-reproduction/test-projects/workaround-package/dist/vitest-link-reproduction.js:7:24)

Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.

 ❯ src/Test.test.jsx (1)
   Γ— renders a button

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  src/Test.test.jsx > renders a button
TypeError: Cannot read properties of null (reading 'useRef')
 ❯ Object.useRef ../../node_modules/react/cjs/react.development.js:1630:21
 ❯ Button ../../node_modules/@mui/base/node/Button/Button.js:54:27
 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:16305:18
 ❯ updateForwardRef node_modules/react-dom/cjs/react-dom.development.js:19226:20
 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21636:16
 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27426:14
 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26560:12
 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26466:5
 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26434:7
 ❯ recoverFromConcurrentError node_modules/react-dom/cjs/react-dom.development.js:25850:20

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

 Test Files  1 failed (1)
      Tests  1 failed (1)
   Start at  11:02:28
   Duration  1.28s (transform 26ms, setup 0ms, collect 437ms, tests 16ms, environment 635ms, prepare 74ms)

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

From my understanding there should be no difference between these two scenarios and the tests should pass. Please let me know if I can provide more info to make it clearer πŸ˜ƒ

Reproduction

https://github.com/ArloL/vitest-link-reproduction

System Info

System:
  OS: macOS 13.5.1
  CPU: (10) arm64 Apple M1 Pro
  Memory: 364.38 MB / 16.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 20.5.1 - /opt/homebrew/bin/node
  Yarn: 1.22.19 - /opt/homebrew/bin/yarn
  npm: 9.8.0 - /opt/homebrew/bin/npm
Browsers:
  Safari: 16.6
npmPackages:
  @vitejs/plugin-react: ^4.0.0 => 4.0.4 
  vite: ^4.1.4 => 4.4.9

Used Package Manager

yarn

Validations

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 15 (6 by maintainers)

Commits related to this issue

Most upvoted comments

It looks like adding @mui/base to resolve.dedupe and removing preserveSymlinks fixes it.

https://stackblitz.com/edit/github-uvwz3x?file=test-projects%2Fvite-project%2Fvite.config.js

Currently @mui/base is coming from (root)/node_modules/@mui/base and thus it ends up pulling react from (root)/node_modules/react while test-projects/vite-project/src/Test.test.jsx is using react from (root)/test-projects/vite-project/node_modules/react. After deduping, @mui/base will come from (root)/test-projects/vite-project/node_modules/@mui/base and thus it can see the same react as vite-project tests.

While debugging and studying the internal of Vite resolve.dedupe, I found that DEBUG=vite:resolve-details might come in handy to see where @mui/base is coming from during Vitest.

Running DEBUG=vite:resolve-details yarn run test
❯ DEBUG=vite:resolve-details yarn run test
yarn run v1.22.19
$ vitest
  vite:resolve-details [package entry] vite -> /home/projects/github-uvwz3x/test-projects/vite-project/node_modules/vite/dist/node/index.js +0ms
  vite:resolve-details [package entry] @vitejs/plugin-react -> /home/projects/github-uvwz3x/test-projects/vite-project/node_modules/@vitejs/plugin-react/dist/index.mjs +14ms
  vite:resolve-details [package entry] vitest -> /home/projects/github-uvwz3x/test-projects/vite-project/node_modules/vitest/dist/index.js +166ms

 DEV  v1.1.0 /home/projects/github-uvwz3x/test-projects/vite-project

  vite:resolve-details [@fs] /@fs/home/projects/github-uvwz3x/test-projects/vite-project/node_modules/vite/dist/client/env.mjs -> /home/projects/github-uvwz3x/test-projects/vite-project/node_modules/vite/dist/client/env.mjs +2s
  vite:resolve-details [@fs] /@fs/home/projects/github-uvwz3x/test-projects/vite-project/node_modules/vite/dist/client/env.mjs -> /home/projects/github-uvwz3x/test-projects/vite-project/node_modules/vite/dist/client/env.mjs +1ms
  vite:resolve-details [fs] /home/projects/github-uvwz3x/test-projects/vite-project/src/Test.test.jsx -> /home/projects/github-uvwz3x/test-projects/vite-project/src/Test.test.jsx +709ms
  vite:resolve-details [fs] /home/projects/github-uvwz3x/test-projects/vite-project/src/Test.test.jsx -> /home/projects/github-uvwz3x/test-projects/vite-project/src/Test.test.jsx +0ms
  vite:resolve-details [package entry] @testing-library/react -> /home/projects/github-uvwz3x/test-projects/vite-project/node_modules/@testing-library/react/dist/index.js +21ms
  vite:resolve-details [package entry] vitest-link-reproduction -> /home/projects/github-uvwz3x/test-projects/workaround-package/dist/vitest-link-reproduction.js +1ms
  vite:resolve-details [node/deep-import] ./jsx-dev-runtime -> /home/projects/github-uvwz3x/test-projects/vite-project/node_modules/react/jsx-dev-runtime.js +1ms
  vite:resolve-details [package entry] vitest -> /home/projects/github-uvwz3x/test-projects/vite-project/node_modules/vitest/dist/index.js +0ms
  vite:resolve-details [fs] /home/projects/github-uvwz3x/test-projects/workaround-package/dist/vitest-link-reproduction.js -> /home/projects/github-uvwz3x/test-projects/workaround-package/dist/vitest-link-reproduction.js +158ms
  vite:resolve-details [fs] /home/projects/github-uvwz3x/test-projects/workaround-package/dist/vitest-link-reproduction.js -> /home/projects/github-uvwz3x/test-projects/workaround-package/dist/vitest-link-reproduction.js +0ms
  vite:resolve-details [package entry] @mui/base -> /home/projects/github-uvwz3x/test-projects/vite-project/node_modules/@mui/base/node/index.js +2ms
  vite:resolve-details [package entry] react -> /home/projects/github-uvwz3x/test-projects/vite-project/node_modules/react/index.js +0ms

 βœ“ src/Test.test.jsx (1)
   βœ“ renders a button

 Test Files  1 passed (1)
      Tests  1 passed (1)
   Start at  17:22:09
   Duration  2.54s (transform 

Maybe this will help, I’m working with a linked custom package that reuses the same dependency and solved it with resolve.mainFields and forcing to use use esmodules first. This seems to fix it for me

  resolve: {
    dedupe: ['mobx'], //mobx is in custom linked package and main package
    mainFields: ['module', 'main'], //this is needed to work
  },