testing-library / preact-hooks-testing-library

Simple and complete Preact hooks testing utilities that encourage good testing practices.
MIT License
57 stars 10 forks source link

Cannot read properties of undefined (reading '__H') #20

Open pitis opened 1 year ago

pitis commented 1 year ago

Hey there guys, I am trying to test some of my custom hooks that work in prod, but don't work with @testing-library/preact-hooks.

I am trying to write some unit tests where I check the height of a banner. Initially I used jest, but now I'm using vitest, which has an API almost identical to jest.

All the tests pass besides the ones on the custom hooks. This seems like a package issue.

Thanks!

image

Stack Info: Preact: 10.11.3 Vite: 4.0.0 Vitest: 0.27.0 @testing-library/preact-hooks: 1.1.0

ositowang commented 1 year ago

we have a similar issue...have you found a solution @pitis

pitis commented 1 year ago

@ositowang I believe the issue is that useState is not mocked. So if you create a way to mock useState, it may work for the other hooks

hckjck commented 1 year ago

https://preactjs.com/guide/v10/preact-testing-library#testing-preact-hooks using renderHook makes the difference.

Cipscis commented 1 year ago

I spent all of yesterday trying to figure out how to get this working. This issue is quite old now, but on the off chance it might help anyone else with this problem here's what I've found.

@testing-library/preact-hooks seems to be intended to be used with older versions of Preact. Because of conflicting peer dependencies, it can't be used alongside the latest version of @testing-library/preact.

However, if you're using Preact 10, then you can import renderHook from @testling-library/preact instead. For my setup, at least, this resolved my issue and I'm not able to test my hooks as expected.

Unfortunately, at the time I'm writing this, the testing documentation for the latest version of Preact currently recommends importing renderHook from the outdated @testing-library/preact-hooks library.