-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Using urql's useMutation hook inside tests yields "TypeError: Cannot read properties of undefined (reading '__H')" error possibly because of currentComponent in getHookState is undefined #3468
Comments
This isn't really a Preact issue, to make this work you would need to add something like this to the vite-config I assume optimizeDeps: {
include: ['preact/hooks', 'preact/compat', 'preact']
}, This because we see a duplicate entry of |
Hi @JoviDeCroock Thanks for your attention! I tried your suggestion optimizeDeps: {
include: ['preact/hooks', 'preact/compat', 'preact']
}, but I got the same error. As a side note I've managed to make the same setup work with Thanks again 😊 |
I tried that solution with your reproduction and it worked perfectly 😅 oh nvm, it only happens in test? we might need something similar for vitest let me explore |
Really appreciate that. I'd happily contribute to this if you direct me to the correct path 😬 |
@EmreErdogan I just ran into this issue with Preact + Vitest and found this solution on a similar issue on the Vitest repo: vitest-dev/vitest#1652 (comment). Forcing the aliased module to load using Stuck this at the top of my
Then I've got a
This is apparently due to the way Vite imports unprocessed modules. I don't really understand the CJS vs ESM madness and wish it wasn't a thing. Weirdly, I only had this problem with |
In a project that aliases react to preact using At the top of
Then, add the following inside the
|
Describe the bug
I'm getting
TypeError: Cannot read properties of undefined (reading '__H')
error on test environment when rendering a component which uses urql'suseMutation
hook. The component renders without a problem on browser.Project structure
@preact/preset-vite + @urql/preact + vitest + @testing-library/preact
About the hook
Link to source code of
useMutation
.Please see the source code of the hook, it looks fine imho. I don't see any problem with its implementation.
As far as I could debug the problem and dug into the source code of Preact hooks, I saw that this line yields the error.
preact/hooks/src/index.js
Line 107 in d4a3013
Because, at the time of calling
getHookState
function,currentComponent
variable isundefined
. So, trying to accesscurrentComponent.__hooks
fails.Error output
To Reproduce
I created a simple repository to show the issue => https://github.com/EmreErdogan/vite-preact-test
Steps to reproduce the behavior:
cd
into it and runnpm install
npm run test
Expected behavior
Test should succeed.
The text was updated successfully, but these errors were encountered: