-
Notifications
You must be signed in to change notification settings - Fork 233
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
Getting not wrapped in act warning using renderHook #406
Comments
The sandbox link you've provided doesn't have a hook in it: In general, I'd say you want to |
you're right, must not have saved. try again-should be good
I tried that in the codesandbox and it worked. But when I try it with the hook I'm working with, the test times out I'm getting
when doing:
I don't want to copy/paste the whole hook in here, but essentially it's doing the same thing as the example hook in the sandbox. Calls an api to fetch an image and before and after sets a What else could I do to debug? |
If it's timing out on Given the test you've show, I'm going to assume you aren't using fake timers and the issue is with the promise. The most common reason I've seen for this is that the call is inside a To check this, wrap the whole Here is an example of what I mean where an error in Also, might be worth just logging out directly before the |
Ok so I've tried and tested a few things. To address your comments first, I did log directly before the setState after the promise resolved and that logged fine. The api call is wrapped in a try catch, but I used So then I logged outside of the async function in the body of the useEffect and saw that the effect/hook was on an infinite loop. After commenting out several function calls I see that the hook runs indefinitely after a setState call. In my IDE I see this through logs running over and over, but in codesandbox, the test just freezes and doesn't complete (it won't log). Here's an example of what I mean. https://codesandbox.io/s/renderhook-setstate-test-q1sz1?file=/src/useGetImages.js Notice the test never finishes/logs. |
in your Generally this would be fine, but because you're then using Again, generally this is fine, but because you then This can be fixed by changing the test to define the argument outside the const imageDTO = [];
const { result } = renderHook(() => useGetImages(imageDTO));
expect(result.current.images).toHaveLength(0); Another alternative (and this will very much depend on your actual requirements and what the rest of the logic in your real hook actually does), is to use a ref to manage which reference to const useGetImages = imageDto => {
const imagesRef = useRef(imageDto);
useEffect(() => {
imagesRef.current = imageDto;
console.log("ran effect");
}, [imageDto]);
console.log("ran hook");
return { images: imagesRef.current };
}; I'll leave it up to you if that is a reasonable alternative. I'll admit it's a bit more complicated and harder to read and it won't implicitly render if |
Yup, okay so that takes care of it.
I'm going to close this, thank you very much for your help. Should have caught the inline variable sooner, but I assumed renderHook wouldn't create it anew for some reason - makes sense that it does though. |
https://codesandbox.io/s/async-surf-8ytrn?file=/src/hook.test.js
Here's a simplified example of a hook I'm using that makes an api call within the hook and also has some state management. Testing the hook from the outside, I have nothing to wrap in
act
.I've tried a few of the solutions https://kentcdodds.com/blog/fix-the-not-wrapped-in-act-warning and https://react-hooks-testing-library.com/reference/api#async-utilities but maybe I missed something.
Also the returned value isn't what I expect (it's still the default false). If I use the
wait
async utils, what would Iwait
for?The text was updated successfully, but these errors were encountered: