Simulating delayed server responses in tests to validate button state on failure #2332
-
Hi I'd like to write a test for scenario:
Mock
Function called on button click
Mock usage
I expect that the test will take around 20 seconds to pass because of the request delay in mock but actually it passes in about couple of seconds which resulted in incorrect test behavior (the 4 step will always pass no matter if My mock is incorrect? If so then how to make it work the expected way? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
Hi. Your test setup looks okay. Just make sure to use fake timers in your testing framework so that your test doesn't actually wait 20s—that's a needless overhead. Put the delay, write the actions , then |
Beta Was this translation helpful? Give feedback.
-
@kettanaito I think there's misunderstanding. I literally want my test to stop and wait for 20sec until request fails. But test executed in couple of seconds. So I think the I understand that 20s is overhead. But it's just for studying purposes. I'm new to MSW and testing, so I'm just playing with it to understand how it works and how I can utilize it. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to test this function:
Looks fine. Both assertions pass. But then I'd like to break a test (so some assertions failed)...to see the behavior of React Testing Library to understand it better.
Both assertions also pass. How can it be possible? Same function with contradictive assertions in catch block |
Beta Was this translation helpful? Give feedback.
-
I think I understood why my test behaves this way. If I right then test enviroment doesn't wait till MSW service finishes the request but executes the test further. Request is in pending state but test engine runs When I change assertion to As a workaround:
Not sure if this okay or bad workaround. |
Beta Was this translation helpful? Give feedback.
You can always delay the mocked response or, even better, use an inverse assertion.