https://cypress.io logo
Using Cypress.intercept with `pathname` and `query` options ignores the `query` option
c

careful-terabyte-63440

05/25/2023, 8:07 AM
Hey everyone 👋 I'm using
cy.intercept
to intercept a route that contains some
query
parameters, but it is not behaving as I am expected. It is intercepting all the routes defined by
pathname
, but not
query
. I experience the issue with the following scenario: I have the following url which I am intercepting -
/json-api/demo/tasks
This url can have 2 different query parameters: -
filter[status]: open
-
filter[completed]: true
I want to intercept all the route for
/json-api/demo/tasks?filter[status]=open
(
/json-api/demo/tasks?filter%5Bstatus%5D=open
) I
intercept
the route using the following options 👇
cy.intercept({
        pathname: '/json-api/demo/tasks',
        query: { 'filter[status]': 'open' },
      }).as('openTasks');
Looking at the E2E test and the Cypress console, the alias
@openTasks
gets attached to the following URLs -
/json-api/demo/tasks?filter%5Bstatus%5D=open
-
/json-api/demo/tasks?filter%5Bcompleted%5D=true
The problem with this behavior is that when I invoke
cy.wait(@openTasks)
, it waits for the wrong request to complete before continuing Replacing the
pathname
and
query
with
path
seems to work, the following options aliases routes properly
cy.intercept({
        path: /\/json-api\/demo\/tasks\?.*filter%5Bstatus%5D=open/,
      }).as('openTasks');
I did find a Github issue that resembles the problem I run into (https://github.com/cypress-io/cypress/issues/16686), but it is a ~so-so quality ticket. I would like to be able to intercept the route using
pathname
and
query
, where am I messing up? 😅 Thank you 🙏