Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cypress is intercepting API calls inconsistently. Ideally a call should be intercepted 22 times but it is getting intercepted by cypress 42 times. #15317

Closed
Ppkd2021 opened this issue Mar 4, 2021 · 8 comments
Labels
stage: needs investigating Someone from Cypress needs to look at this stale no activity on this issue for a long period topic: cy.intercept() type: unexpected behavior User expected result, but got another

Comments

@Ppkd2021
Copy link

Ppkd2021 commented Mar 4, 2021

Current behavior

There is an API which is getting called in frontend multiple time(22 times) when we are trying to intercept each call for that respective api by cypress then it is intercepting inconsistently, sometimes 42 or 44 times.

Desired behavior

It should intercept that API call 22 times.

Test code to reproduce

describe('cypress api intercept bug', () => {
  it('ListInsightViewMetricsByInsightViewId api', () => {

	let i =0
	cy.intercept(`POST`, `RouteURL`, req => {
		
        if (req.body.query.includes(`ListInsightViewMetricsByInsightViewId`)) {	
          console.log(req.body);
		  console.log(`request found`,i);
              i++ 
          //req.alias = `${query}-${i}`;
        }
      });
    cy.visit('Application URL');
	
    cy
		.get(`[type="submit"]`,{timeout:30000})
		.as(`BtnSubmit`)
		.should(`be.disabled`)
		.get(`[data-placeholder="Email"]`,{timeout:30000})
		.type(`Email`)
		.get(`[data-placeholder="Password"]`)
		.type(`Password`)
		.get(`@BtnSubmit`)
		.should(`be.enabled`)
		.click()
	
		//cy.wait(`@ListCampaignGroups`).then(req=>console.log(req))
    .get(`.brand-list-wrapper > :nth-child(1)`,{timeout:60000})
	
    .click()
	
    .get(`.link-btn`)
    .click()
	cy.wait(`@ListInsightViewMetricsByInsightViewId`,{timeout:80000}).then(req=>console.log(req))
  })
})  

Versions

@Ppkd2021
Copy link
Author

Ppkd2021 commented Mar 4, 2021

Application intercepting 22 times
image (7)

Cypress intercepting 43 times
image (4)

image (5)

@jennifer-shehane
Copy link
Member

@Ppkd2021 We've recreated the situation from the repo you previously provided and confirmed the behavior you are saying is happening. So this appears to be a bug or unexpected behavior that one of our engineers will have to evaluate when we have time. Thanks for taking the time to report.

@jennifer-shehane
Copy link
Member

Looking further at this, it appears that the graphql calls are doubled regardless of the cy.intercept() definition, so it may not be related to intercept and be related to something else.

@jennifer-shehane jennifer-shehane added type: unexpected behavior User expected result, but got another stage: needs investigating Someone from Cypress needs to look at this and removed topic: cy.intercept() labels Mar 4, 2021
@bahmutov
Copy link
Contributor

bahmutov commented Mar 5, 2021

Reproduction

The same problem in v6.5.0 v6.6.0 and branch 7.0-release

https://github.com/cypress-io/cypress-example-recipes/tree/triple-intercept folder examples/server-communication__wait-for-api

  • install root dependencies with npm install
  • in the folder itself npm run dev

The following test there shows double or sometimes triple intercept execution for a single fetch from the application

it('fails because the API is not ready', () => {
  cy.visit('/')
  cy.intercept({
    pathname: '/greeting',
  },
  {
    forceNetworkError: true,
  }).as('greeting')

  cy.get('#get-api-response').click()
})

it('fails because the API is not ready (with log)', () => {
  cy.visit('/')
  cy.intercept({
    pathname: '/greeting',
  },
  (req) => {
    console.log('in the greeting intercept')
    console.log(JSON.stringify(req))
    req.reply({ forceNetworkError: true })
  })

  cy.get('#get-api-response').click()
})

Images

Screen Shot 2021-03-05 at 1 08 52 PM

Screen Shot 2021-03-05 at 1 24 23 PM

@flotwig
Copy link
Contributor

flotwig commented Mar 5, 2021

@bahmutov I don't think this is related - those requests are probably actually being sent. I'm not sure if the Network tab will show it, but Chrome will re-send a request that fails due to network error a few times, so that is probably why you are seeing this behavior.

Check out the 0_network_error_handling_spec e2e test if you're curious, Chrome's behavior w.r.t retrying on network errors is tested in great detail there.

@vrknetha
Copy link

vrknetha commented Mar 8, 2021

@jennifer-shehane Thank you for having a look at it. Please let us know if you need anything else which helps fixing this issue.

@cypress-app-bot
Copy link
Collaborator

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

@cypress-app-bot cypress-app-bot added the stale no activity on this issue for a long period label May 17, 2023
@cypress-app-bot
Copy link
Collaborator

This issue has been closed due to inactivity.

@cypress-app-bot cypress-app-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage: needs investigating Someone from Cypress needs to look at this stale no activity on this issue for a long period topic: cy.intercept() type: unexpected behavior User expected result, but got another
Projects
None yet
Development

No branches or pull requests

6 participants