3 ms·
Hooks are great! Although, when I see code snippets on the internet I always wonder how do people test this. The fact that dependencies are declared as import
by knuthsat 5y ago
Hooks are great!
Although, when I see code snippets on the internet I always wonder how do people test this.
The fact that dependencies are declared as imports and all the logic is inside the component function, it feels like you have to shuffle things around just to make the thing runnable outside of React.
- HuntingMoa 5y agoreact-testing-library provides a helper to test hooks in relative isolation - renderHook.
- bayesian_horse 5y agoThere are special tools to "render" react components in node-based test runners. Of course, you need to write your components in such a way that they are easy to isolate. You can also make use of composition and maybe even mock out components.
- gherkinnn 5y agoUnless you're talking about library-level hooks, there's rarely a need to test them directly. I'd mostly consider them implementation detail. Testing the components themselves is ample.
- loh 5y ago> how do people test this You can find the tests for these hooks here: https://github.com/Molecule-dev/molecule-app/tree/6e2456e216767b529c32d16a6952e1ff0858250c/src/hooks/__tests__ https://github.com/Molecule-dev/molecule-app/tree/6e2456e216... Is that what you're asking about?
- steve_adams_86 5y agoOpinion: Although you can test hooks in isolation, I find they tend to be fairly primitive and testing them in combination with components can be more useful. In isolation, unless you have very complex hooks, it’s a bit like you’re testing react or JavaScript themselves. When testing a component which uses the hook, I find you get to test the behaviour and expectations a little better - it’s close to a real use case.