Provide a way to trigger useEffect from tests
See original GitHub issueHello,
I tried testing components that use the cool new hooks API, but useEffect
doesn’t seem to work with the test renderer.
Here’s a small failling Jest test:
import React, { useEffect } from "react";
import { create as render } from "react-test-renderer";
it("calls effect", () => {
return new Promise(resolve => {
render(<EffectfulComponent effect={resolve} />);
});
});
function EffectfulComponent({ effect }) {
useEffect(effect);
return null;
}
And here’s a minimal reproducing repo: https://github.com/skidding/react-test-useeffect
Note that other use APIs seemed to work (eg.
useContext
).
Issue Analytics
- State:
- Created 5 years ago
- Reactions:27
- Comments:23 (10 by maintainers)
Top Results From Across the Web
Trigger useEffect in Jest and Enzyme testing - Stack Overflow
We want to get the test block to await the next tick, that is, wait for the call stack and pending promises to...
Read more >useEffect – How to test React Effect Hooks - cultivate
The rest of the test is straightforward: Render the component. Get the list of students and verify that there are no list items...
Read more >Testing useEffect and Redux Hooks using Enzyme - Medium
To test the component update useEffect hook you'd simply trigger state updates and check for effects in rendered elements. Redux hooks can be ......
Read more >A Complete Guide to Testing React Hooks - Toptal
This article explores how to test React Hooks and outlines an eight-step testing plan you could employ to test your own projects.
Read more >How to Test React.useEffect | Epic React by Kent C. Dodds
How does the user make that code run? Make your test do that. That's it. That's the secret. The trick is discovering what...
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
We should have something to trigger them.
I think you don’t need such a hassle for this. Jest would automock if you create a file
<root>/__mocks__/react.js
and in there you can just…This is a great workaround as you don’t need to touch any code when this is somehow fixed, you will just remove the mock.