NoriSte/cypress-wait-until

WaitUntill works only with Cypress methods

JonahKK opened this issue · 9 comments

It is more like a question, or a request for clarification. I was not able to find an answer in docs.
So in my case I have a class with method that is responsible for fetching some data from API. This method uses waitUntil and returns a Cypress' promise.
So now in test I want to fetch this data till the returned json will have certain value. In order to do it I wrote:
cy.waitUntil(() => apiClient.get(item.id).then(i => i.items[0].Name === item.items[1].Name));
What happened during execution was Cypress waited indefinitely long for this wait. Timeout was never raised (although time passed).
So in my second attempt I wrapped the whole expression in cy.wrap:
cy.waitUntil(() => cy.wrap(apiClient.get(item.id).then(i => i.items[0].Name === item.items[1].Name)););
In this form, the wait worked flawlessly.
My question is, whether waitUntil is limited only to cy commands, or is it some kind of the defect. Maybe usage of two waitUntil is the issue here?

Hi @JonahKK
Thanks for asking, could you share a minimal repository to play with, please?
Thanks
Stefano

Nested cy.waitUntil calls should work as expected 😉

@JonahKK, up 😉

@NoriSte Sorry about the delay, had a lot on my mind recently. I will try to prepare the example this week.

Perfect, thanks!!! I appreciate it 😊

I have been experimenting with the different setups, trying to reproduce the exact error I've been describing, yet without luck. Obviously I cannot share code from work.
All I have observed was that waitUntil nested inside of another wait can take a lot of time, before the timeout happens. It might work as expected, yet with default timeouts (5 seconds) it takes over 60 seconds to fail!
image
Maybe I was experiencing this extended wait time, that made me think there is an error - I use bigger timeouts in my production code.

Thanks for the feedback @JonahKK!! 😊

All I have observed was that waitUntil nested inside of another wait can take a lot of time, before the timeout happens. It might work as expected, yet with default timeouts (5 seconds) it takes over 60 seconds to fail!

Could you share a repo with this example, please? I didn't notice it in my tests 🤔

Please check the https://github.com/JonahKK/cypress-test-tiny/tree/wait-until. Just open the Cypress runner and launch the spec. The test should timeout after 70 seconds (unless you will be lucky and have a match in the wait 😉)

You're right, I hadn't realized it until now... well, how waitUntil works:

  1. it runs the checkFunction
  2. if the result is falsy, it waits (through cy.wait)
  3. return to step n°1 25 times (by default, it's the result of 5000/200, the default timeout, and the default interval)

Then, what happens when two waitUntil calls are nested

  1. parent: it runs the checkFunction
    1.1. child: it runs the checkFunction
    1.2. child: if the result is falsy, it waits (through cy.wait)
    1.3. child: return to step n°1.1 25 times
  2. parent: if the result is falsy, it waits (through cy.wait)
  3. return to step n°1 25 times

so the parent waitUntil call will retry 25 times (by default) but every time the child waitUntil call runs 25 retries, completely breaking the concept of "time" of the parent call.

I'm going to add this note to the README, thanks a lot for the dedication @JonahKK!!!!!! ❤️