cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.06k stars 3.19k forks source link

Waiting for an intercepted graphql request yields the request/response of a prior request #30507

Open betawind opened 2 weeks ago

betawind commented 2 weeks ago

Current behavior

I'm using the pattern defined below to intercept and assert on graphql requests and responses: https://docs.cypress.io/app/guides/network-requests#Alias-multiple-queries-or-mutations

The test runs the cy.intercept in a beforeEach, and includes a single aliasQuery call in the route handler function.

The page uses Graphql to populate data in a table, and interacting with the page results in additional requests to Graphql for new data. When using cy.wait on a request that occurs after several have already occurred, the resultant request object is for a prior request. I can use cy.wait multiple times to get each request's details sequentially.

Example

  1. Before the page loads I use cy.intercept to create an alias for the getData Graphql operation called gqlGetDataQuery
  2. Page loads, Graphql operation getData is called
  3. Make a change on the page that triggers a new request to Graphql operation getData
  4. Make another change on the page that triggers a new request to Graphql operation getData
  5. cy.wait('gqlGetDataQuery') results in the data for step 2
  6. another call to cy.wait('gqlGetDataQuery') results in the data for step 3
  7. cy.wait('gqlGetDataQuery') results in the data for step 4###

Desired behavior

cy.wait returns the request/response for the most recent Graphql query

Test code to reproduce

NOTE: The test code uses Cypress Dashboard and it's Graphql endpoint to demonstrate the issue. To run this you'll need to do the following:

  1. Log in to Cypress Dashboard
  2. Copy the "cy_dashboard" cookie using developer tools and update the "cyDashboardCookie" variable below
  3. Select a project to run this, copy the projectId from the URL, and update the "projectId" variable below

REPO WITH THE ISSUE: https://github.com/betawind/cypress-30507

Cypress Version

13.15.1

Node version

v20.17.0

Operating System

macOS 15.1

Debug Logs

No response

Other

No response

Tzinov15 commented 1 week ago

I believe we may be hitting something similar with this, so commenting mainly to follow development, but what we're seeing is an apparent issue where cy.wait(queryAlias.all) will return the correct number of intercepted requests (4) when we send back a stub in the response (ie: req.reply()) but will for some reason respond with 6 when we don't send back a stub and allow the request to pass through to the server

attached is the result of calling cy.get('${alias}.all')

Screenshot 2024-11-05 at 6 22 07 PM

Potentially related to the problem, I am seeing redundant ids with "interceptedRequest562" and "interceptedRequest597" showing up twice