how to use Cypress route with query strings

Viewed 2452

Im trying to capture a request using Cypress route but for some reason it just isnt working. There is a query string in the url which may be the reason why it isnt working. How do I capture the request using the cy.route?

cy.fixture("timeslots").then((json) => {
      cy.route(
        "GET",
        `${Cypress.env("apiUrl")}availability?startDate*`,
        json
      );
    });

Ive tried a ton of different variation on the above but just cant get it working. The actual url is:

baseApiUrl/availability?startDate=Thu%2C+02+Jul+2020+13%3A16%3A48+GMT&locationId=55cfb68b-c9b5-ea11-9b05-2818783b5d9b&typeId=a2cfb68b-c9b5-ea11-9b05-2818783b5d9b&isNewCommander=false&timeZone=Mars%2FSolaria

Id really appreciate a hand on this

2 Answers

Hopefully this is useful to other but this issue was solved by using a regex instead of the globster.

cy.route("GET", new RegExp(/\bavailability\?startDate=\b/), json);

Just wondering if you initialized cy.server() before cy.fixture("timeslots")...? (remember you have to start a server before routing any response to cy.route() and to change the behavior of network requests if you need that ;) ). Also, you have to set up your routes to listen before visiting the application.

Related the query string from the url, you can use any globster to check if it matches and pass, so I believe this glob should work. **/availability?**:

 cy.server();
 cy.route('GET', '**/availability?**', 'fixture:timeslots');
 cy.visit('/')
Related