You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was trying to test behavior of the useAsync hook using the renderHook from the testing-library/react-hooks, and found the following issues (which is probably I'm trying to use the library in a wrong way).
First, when hook returns it's initial state, it returns initialValue (as expected), but it also indicates that the promise has been already resolved:
it('should set into proper initial state',async()=>{constasyncFunc=()=>Promise.resolve(2);const{ result }=renderHook(()=>useAsync(asyncFunc,{initialValue: 1}));expect(result.current.value).toEqual(1);expect(result.current.isResolved).toBe(false);// FAILS, ALREADY RESOLVED});
I expected it to be either { data: 1, isResolved: false }, or { data: 2, isResolved: true }.
Second issue, is that for some reason the waitForNextUpdate never resolve:
it('should resolve resolved promise',async()=>{// already resolvedconstasyncFunc=()=>Promise.resolve(2);const{ result, waitForNextUpdate }=renderHook(()=>useAsync(asyncFunc,{initialValue: 1}));expect(result.current.value).toEqual(1);awaitwaitForNextUpdate();// NEVER RESOLVE});it('should resolve asynchronous promise',async()=>{// will resolve in 50msconstasyncFunc=()=>newPromise(resolve=>setTimeout(resolve,50)).then(()=>2);const{ result, waitForNextUpdate }=renderHook(()=>useAsync(asyncFunc,{initialValue: 1}));expect(result.current.value).toEqual(1);awaitwaitForNextUpdate();// NEVER RESOLVE});
This is probably related to the first issue - promise is in resolved state already but the value from the promise wasn't set.
The text was updated successfully, but these errors were encountered:
I'm sorry, I had intention to post it to react-async, posted by mistake here. I believe the issue is with react-async, not @testing-library/react-hooks.
I was trying to test behavior of the
useAsync
hook using therenderHook
from the testing-library/react-hooks
, and found the following issues (which is probably I'm trying to use the library in a wrong way).First, when hook returns it's initial state, it returns initialValue (as expected), but it also indicates that the promise has been already resolved:
I expected it to be either
{ data: 1, isResolved: false }
, or{ data: 2, isResolved: true }
.Second issue, is that for some reason the
waitForNextUpdate
never resolve:This is probably related to the first issue - promise is in resolved state already but the value from the promise wasn't set.
The text was updated successfully, but these errors were encountered: