`renderHook` from /dom/pure has global side-effects
See original GitHub issuereact-hooks-testing-library
version: 5.0.0react-test-renderer
version: not installedreact
version: 17.0.1node
version: 17.0.1npm
(oryarn
) version:yarn@1.22.0
Relevant code or config:
const { renderHook } = require("@testing-library/react-hooks/dom/pure");
let consoleError;
beforeEach(() => {
consoleError = console.error;
});
afterEach(() => {
if (console.error !== consoleError) {
throw new Error("Did not cleanup console.error");
}
});
test("pure", () => {
function useHook() {
return useState(false);
}
const { result } = renderHook(() => useHook(), {});
});
What you did:
Use @testing-library/dom/pure
in karma-mocha
though the problem happens in arbitrary testing frameworks I suspect
What happened:
console.error
was not cleaned up
Reproduction:
https://github.com/eps1lon/testing-library-react-hooks-impure
Problem description:
We check for mistakes in afterEach
if a test was not properly cleaned up. One of those checks looks at console.error
.
Suggested solution:
renderHook
probably has some global side-effects. These should be documented by explaining why they exists and how to disable them or work around them.
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (10 by maintainers)
Top Results From Across the Web
API Reference | React Hooks Testing Library
The renderHook function returns an object that has the following properties: ... hooks will not have any unintended side-effects on the following tests....
Read more >Using the Effect Hook - React
The Effect Hook lets you perform side effects in function components: ... This is why in React classes, we put side effects into...
Read more >Hooks Tests Acting Up - Cogent
As a result, we need to use something like renderHook() to test them. Async code executing after a test has finished will result...
Read more >A Complete Guide to Testing React Hooks - Toptal
This is because we want to replicate the real world as much as possible. We can't test the hook correctly if we return...
Read more >react-hooks-testing-library - npm
Your hook is defined along side a component and is only used there; Your hook is easy to test by just testing the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is just not reality but I can’t tell you that. I can only show you that. Maybe me, as a testing-library maintainer, being confused by this behavior helps you understand why this mentality is problematic.
So we still have the documentation issue. This behavior is underdocumented which is especially problematic since it’s not far fetched to assume that these libraries work similarly considering they are under the same org.
Anyway, I’ll be using a thiner wrapper in the future that doesn’t require to learn a new library. Getting co-workers on board with testing is hard enough. Expecting them to learn yet another library is not viable.
This is where I think our philosophies diverge a bit. While this library was inspired by, and much of the original implementation did indeed come from
react-testing-library
, at the end of the day they are trying to solve different problems which is why they have different implementation and behaviour. I believe that any time you introduce a new library into your project you should take the time to learn about it and it’s behaviour.If all this library did was wrap the hook call in a component and remove a handful of lines out of your test file then I don’t think we would exist at all.
Fundamentally, hooks are not components and components are not hooks. They have different use cases and different lifecycles. The more we see users moving their complex logic into custom hooks and the more we see people wanting to ensure all the ways that hook will behave, the more use cases we are going to try to support.
I’m not sure what this basis for this commentary is. I don’t believe that that a user wanting to test that triggering an effect that causes an error to be thrown is testing implementation detail. I do believe that the user having to deal with the console output from our use of an error boundary is an implementation detail and is what we were trying to trying to protect them from.
If React didn’t log the error and we didn’t patch
console.error
would you still have a concern? Is the problem just that we messed up and don’t restore the the console for the pure import (a legitimate issue with the current version and one we are attempting to fix).Is the concern that we have have
result.error
instead of allowing the error to bubble out to the test code? I’ve mentioned a few times that I’d love to remove it, but I haven’t been able to have it raise out in all instances where I believe it would be important. It was implemented beforeact
existed so it may be possible now and I am planning on trying to try it it again, but I stumble somewhere every time I’ve tried before, usually around async update to state that case the error on a subsequent render.Please don’t feel like I’m being dismissive of your concerns here. I’m genuinely trying to understand and think about what the implications of this discussion might be to the library in the future.