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

TypeError: Cannot read properties of null (reading 'useContext') #124

Closed
1 task done
dolie opened this issue Jun 17, 2024 · 5 comments
Closed
1 task done

TypeError: Cannot read properties of null (reading 'useContext') #124

dolie opened this issue Jun 17, 2024 · 5 comments

Comments

@dolie
Copy link

dolie commented Jun 17, 2024

  • Check if updating to the latest Preact version resolves the issue

Describe the bug
Hello,

I'm not able to run Preact components using SWR, in a test environnement.
In dev/prod environnement Preact handles perfectly SWR, but not in test environnement.
When I launch the test, it returns the error :
TypeError: Cannot read properties of null (reading 'useContext')

To Reproduce

Barebone example : https://github.com/dolie/preact-swr-vitest-bug

Steps to reproduce the behavior:

  1. git clone git@github.com:dolie/preact-swr-vitest-bug.git
  2. pnpm i
  3. pnpm vitest

Expected behavior
Preact handles SWR in a test environnement, as it is running in dev/prod environnement.

Additional Context

SWR
Vitest
MSW
Testing-library

339088817-e5442f68-71b5-4754-bb6f-2daf198164ec-2
@JoviDeCroock
Copy link
Member

JoviDeCroock commented Jun 17, 2024

As your stacktrace sugggests this error is happening in react.development.js which means that you are trying to import useContext from React rather than Preact. I don't think this bug belongs here, it might be due to preset vite but def not on the core repo

@JoviDeCroock JoviDeCroock transferred this issue from preactjs/preact Jun 17, 2024
@rschristian
Copy link
Member

rschristian commented Jun 17, 2024

Vitest unfortunately has problems aliasing, this is something we've seen a few times. For reference, vitest-dev/vitest#5004, vitest-dev/vitest#4365 (comment), potentially vitest-dev/vitest#1910

I think this is something Vitest needs to address, not us here.

@dolie
Copy link
Author

dolie commented Jun 17, 2024

Thank you for you fast answer !

Ok, so I added preact/compat using pnpm aliases, but it throws a new error about useSyncExternalStore ?

Capture d’écran 2024-06-17 à 17 26 06

@rschristian
Copy link
Member

That error says you're loading multiple copies of Preact at once; hooks, in both Preact and React, require that the library is loaded as singleton (only once).

Probably the dual-package hazard: vitest-dev/vitest#4365. Again, something that unfortunately needs to be addressed in Vitest (as far as I am aware).

@dolie
Copy link
Author

dolie commented Jun 20, 2024

Vitest team found a workaround😉
I updated the repo with it.
Thank you for your help @rschristian @JoviDeCroock 🙏

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

No branches or pull requests

3 participants