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've written a small test case demonstrating issues I've had when combining user event (press) -> promise -> setState -> useEffect. The test case fails for RNTL but passes on @testing-library/react.
test.only('async event with fireEvent',async()=>{constComp=({ onPress }: {onPress: ()=>void})=>{const[state,setState]=React.useState(false);React.useEffect(()=>{console.log('useEffect');if(state){console.log('onPress');onPress();}},[state,onPress]);return(<PressableonPress={async()=>{console.log('waiting');awaitPromise.resolve();console.log('setting');setState(true);}}><Text>Trigger</Text></Pressable>);};constspy=jest.fn();const{ getByText }=render(<ComponPress={spy}/>);fireEvent.press(getByText('Trigger'));awaitwaitFor(()=>{console.log('checking');expect(spy).toHaveBeenCalled();});});
Logs go:
useEffect
waiting
checking
setting
checking
... a bunch of other `checking`
checking
useEffect
onPress
Note that the final onPress call (the one that we're waiting for) happens after the last check.
I'll do more investigation to figure out what's happening, but if anyone manage to fix it, feel free to do so :) I kind of expect this issue to be related to a lot of the ones we've had around problems with waitFor, or async act ....
Versions
Latest main, commit of cb46ae2689cc661879e8609766041bf93f1d3137.
The text was updated successfully, but these errors were encountered:
Changing the way we handle async update to the way it's handled in the newer versions of @testing-library/react, i.e. dropping the await act and changing the global IS_REACT_ACT_ENVIRONMENT variable works (not changing the global variable still works but leads to some console.error.
I'll need to investigate a bit more on what are the consequences of this change. @mdjastrzebski we officially support react@>16, right? Have we considered running both react@17 and react@18 in the CI?
@AugustinLF that's interesting, I will need some more time to understand that RTL approach here. Regarding support we run our test against react@18 (matching RN version). Previously we ran it against previous RN versions, some of which were react@17.
If we want to support many RN version we can duplicate examples/basic with various RN & React version and plug them into proper CI job. Note that example/* tests are run against published RNTL version(s), not local one.
Describe the bug
I've written a small test case demonstrating issues I've had when combining
user event (press) -> promise -> setState -> useEffect
. The test case fails for RNTL but passes on@testing-library/react
.Logs go:
Note that the final
onPress
call (the one that we're waiting for) happens after the last check.Expected behavior
It works as expected on web, with those logs
I'll do more investigation to figure out what's happening, but if anyone manage to fix it, feel free to do so :) I kind of expect this issue to be related to a lot of the ones we've had around problems with
waitFor
, orasync act ...
.Versions
Latest
main
, commit ofcb46ae2689cc661879e8609766041bf93f1d3137
.The text was updated successfully, but these errors were encountered: