cypress: cy.wait("@route") sometimes does not catch route
Current behavior:
Occasionally (no, I do not have a reproducible example), when I wait for a route the route will complete before the call to cy.wait("@theRoute") is made, then the wait will time out. This happens a very small fraction (<1%) of the time and the only consistency that I can find is it happens after the test .click()s something, then immediately waits for a route, but it doesn’t seen to be any tests in particular. It is like the route returns too fast for cypress to see.
See screenshot below - the line above the failed wait is the route it is supposed to wait for.

Desired behavior:
I think this is obvious
Steps to reproduce:
I don’t have any - I know this is super helpful.
Versions
Cypress 3.1.0 Chrome 70
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 27
- Comments: 45 (10 by maintainers)
This was closed due to no one providing a reproducible example that we can run locally to recreate the failure. We will reopen if someone provides a reproducible example - app and test code that can be run to see the problem.
Unfortunately we’ll have to close this issue if no reproducible example is provided. Can anyone provide a way to reproduce this?
Experiencing the same issue. Please reopen the issue
The above code can just be written as
@FilepCsaba Your issues with the waits is the same as @gituser3000, the routes should be defined BEFORE the calls to the routes happen.
Workaround
You should NEVER have a
cy.wait()directly after acy.route(). This will NEVER work. The wait will ALWAYS time out. Think ofcy.route()as essentially just setting up a LISTENER to listen to those XHR requests.It should be like this:
I think I finally found a reproducible example. Github’s login response is very fast such that this test always fail on me.
Reddit is not as fast as Github so a similar test always pass.
I was hoping to capture this GitHub login request.
This is how the test pass in Reddit. https://i.stack.imgur.com/vKwAj.png
The result is the same even if I use:
Also having this issue! The request is not captured by cypress so it’s not mocked and the live API is called instead.
There is now a reproducible example so the thread should be reopened?
I also had this issue several times. I made the route pattern very generic, which must have matched.
I think this happens because of, that your
cy.server&cy.requestcalls are at the beginning of your spec. I suppose the app loads faster than cypress can hook into for listen to the requests. I hope this issue could be fixed with #687Sadly because of this undeterministic issue, I can’t use the wait on request for first reuqest(s) from my app.
While we were never given a clear reproducible example of this bug (I was never able to reproduce from https://github.com/cypress-io/cypress/issues/2700#issuecomment-629597892 either), I believe this was likely occuring for some people. There were some documented cases of similar bugs here: https://github.com/cypress-io/cypress/issues/5999
Resolution
We suggest migrating
cy.route()to usecy.intercept().cy.route()will be deprecated in a future version. Moving to this new command resolved many documented reproducible examples of ‘wait’ never catching a route in https://github.com/cypress-io/cypress/issues/5999 and may resolve your issue also.If you’re experiencing a bug after upgrading to
cy.intercept(), please open a new issue with a fully reproducible example that we can run.@jennifer-shehane
Steps to reproduce: 1- Have a function like the one below:
2- Call it once 3- Call it another time in the same testcase
The cy.wait(“@ApiCall”) will think that the API has been already called in the second function call and the code will continue running (because of the first time we called the function). Im assuming it has something to do with some flag that’s being saved.
+1 - we see this pretty frequently and have to workaround with a bunch of assertions that elements on the page exist as a result of a route rendering.
Code example:
Yet, often this fails when the
getSalesroute starts and finishes rendering before the wait call starts. If we take out the wait because presumably the route renders quickly enough, we get flaky results because the route is not always fast enough for the subsequent snapshot.When this fails, we see the
getSalesroute has already completed before cypress begins waiting for the route. To answer some troubleshooting questions from earlier in this thread; we are calling cy.server() and defining the route first thing in the test and the route endpoint does not change, it is always the same.We need a reliable way to wait for a request to finish loading, whether it’s fast or slow. It’d be nice if we could have some kind of listener or way to call a wait before the command that will trigger the request, so it’s for certain waiting before the request starts.
We have similar issue, however wait ignores 100% of requests like
PUT,POSTand others (onlygetis okay). Also i have the problem on my local pc, not even CI. we tried to catch requests with:or
or
and other variations of this command. Example of request:
XHR request is displayed in cypress panel, however it’s completely ignored by wait.
Our app is split in two parts: 1st part is backbone, 2nd part is angular 7. The issue is reproducible in each. Requests go through nginx.
Also had this issue once in a while even though my route pattern is already very generic like below.
The app only starts hitting the backend API after I type more than 3 letters of the search term. However, even with this delayed start of API request from the app, Cypress still wasn’t able to catch the route. Cypress was still waiting until it timed out even though the app has already received the response from the API and has already displayed it in UI.
@jennifer-shehane What is the reasoning behind adding an assertion before the wait?
I have a set of tests that fails reliably on at least one of the cy.wait(‘@fixture’) calls, but it isn’t always the same call. This spec is 368 lines long, and it only started failing when I added a couple of new “it” blocks. Each of the blocks passes individually when run with .only. This leads me to believe it’s a timing issue or memory leak within Cypress.
Update: After noticing that more and more cy.wait calls were failing as I went along, I just refreshed the whole chrome page instead of letting cypress re-run the tests. All tests passed. I’m voting memory leak.
I’m seeing similar issues. For us it is intermittent. We have about 7 out of 55 specs which are susceptible to this type of failure. Pretty reliably at least one of them will fail on every CI run. Running the tests individually always seems to work.
@jennifer-shehane, I have a reproducible example if you wanted to get on a call with me, I can share. I’m seeing in the network request in cypress’s console output as well as the network tab of Chrome’s devtools.
Cypress’s output window (left pane) shows a route object being setup (matching pattern), in the test object I never see the XHR entry. More over I have the exact same form that fires off the XHR request working 100% of the time, and does show a XHR entry in Cypress’s left pane output.
Many people have problems but you are simply closing issues, cool
I was having this same issue just now. My issue was that I was using
fetchinstead of XHRin my
cypress.jsonI had to add"experimentalFetchPolyfill":trueand everything seemed to work withcy.route()andcy.wait()I tried using
cy.route2withexperimentalNetworkStubbingenabled and that didn’t work.You should see your network requests appear in the steps pane. If they don’t, you might not have the Fetch polyfill enabled.
as @jennifer-shehane mentioned the order of ops is:
cy.server()somewhere (in yourbeforeor in your command you want to sniff network requests on)cy.route(<METHOD>, <URLMatcher>, <Options>).as("mySpecialAlias")before invoking the click event or button that triggers your ReST callcy.wait(["@myAlias",...,"@myOtherAlias"])<-- this handles multiple calls in one waiter with similar behavior toPromise.all()hope this helps!
@clock-rick I’d probably add an assertion about the url before defining the wait.