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

Cancelled network requests fail to resolve with cy.wait #19326

Open
jeremydorne opened this issue Dec 10, 2021 · 14 comments
Open

Cancelled network requests fail to resolve with cy.wait #19326

jeremydorne opened this issue Dec 10, 2021 · 14 comments
Labels
stage: needs investigating Someone from Cypress needs to look at this topic: cy.intercept()

Comments

@jeremydorne
Copy link
Contributor

jeremydorne commented Dec 10, 2021

Current behavior

If a network request is aliased and the network request is cancelled by the browser, cy.wait will timeout waiting for the network request.

Desired behavior

cy.wait should complete with a failed network request and should convey that the request was cancelled.

Test code to reproduce

// Intercept an outbound request with `cy.intercept` and set it to an alias
cy.intercept("route-to-request", req => {
     req.id = "cancelled-request-id"
     req.reply()
}

// Redirect to a new page before the request resolves
cy.visit('http://localhost:3000/path-to-new-url')

// Wait for the cancelled request to resolve
cy.wait("@cancelled-request-id")

Cypress Version

9.1.1

Other

No response

@jeremydorne
Copy link
Contributor Author

Poking around in the debugger, once cancelled intercepted requests get a response, they are marked with the state ResponseReceived but will never reach the Complete state.

@JosiahAllen
Copy link

Hitting this issue as well, seems to block my ability to catch subsequent completed requests as well.

@laurence-myers
Copy link

This is a problem for Single Page Applications. For example, in a Vue app that makes GraphQL requests using Apollo, navigating to different page will cancel any requests in progress.

If the tests are written as full E2E tests, hitting a real API and database, and each test clears & seeds data in the database, you might start to see intermittent database deadlocks. This is because some GraphQL query is still in progress and accessing the database, while the DB is attempting to truncate all tables.

When dealing with a large number of existing (legacy) tests, the quick solution is to intercept every GraphQL request in a beforeEach() hook, and wait for each request in an afterEach() hook. Unfortunately, this doesn't work, due to this issue with cancelled requests. A cancelled request will be intercepted, but attempting to wait for it will always fail.

@flotwig
Copy link
Contributor

flotwig commented Dec 15, 2022

@mjhenkes this is fixed by #24709, right?

@NavyMint
Copy link

@flotwig looks like yes but still reproducible, im using 9.7 version

@flotwig
Copy link
Contributor

flotwig commented Dec 15, 2022

@NavyMint try updating to Cypress 12, which is where that fix landed. We don't typically backport fixes to older versions.

@Sam152
Copy link

Sam152 commented Feb 17, 2023

I seem to be able to reproduce this with 12.5.1 with the following test case:

context('Waiting on cancelled requests', () => {

  it('waiting will resolve when a request is cancelled', () => {
    cy.intercept(/jsonplaceholder.cypress.io/).as('intercepted-example')

    cy.visit('https://example.cypress.io/commands/network-requests')
    cy.get('.network-btn').click();
    cy.visit('https://example.cypress.io/commands/network-requests');

    cy.wait('@intercepted-example');
  });
});

Screenshot 2023-02-17 at 12 12 51 pm

@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 Aug 17, 2023
@NogDar
Copy link

NogDar commented Aug 22, 2023

Issue still occurs on 12.17.4

@piotrpalek
Copy link

any updates here? 🙏

@AshMcConnell
Copy link

I'm having this issue on the latest Cypress. We have a field that we type in and it fire off a network request with every few chars. It's pretty normal to have cancelled requests. We get this when the cancelled request happens: -

image

Is there any way to ignore it?

@krzysieknd
Copy link

I have the same problem, is there any solution to ignore the canceled request?

@esetnik
Copy link
Contributor

esetnik commented Sep 20, 2024

I have the same issue. Has anyone found a workaround?

@vaclavGabriel
Copy link

I have the same issue. Has anyone found a workaround?

My workaround is to mock the status code of such requests:

// adjust status code for each request to prevent
// cypress from skipping requests cancelled by the browser
cy.intercept(/trpc/, (req) => {
	if (req.url.includes('search.searchAll')) {
		req.alias = 'Search'
		req.reply({ statusCode: 200 })
	}
})

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 topic: cy.intercept()
Projects
None yet
Development

No branches or pull requests