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

cy.intercept() not overriding existing alias properly #20728

Closed
sanjayupasi opened this issue Mar 22, 2022 · 6 comments
Closed

cy.intercept() not overriding existing alias properly #20728

sanjayupasi opened this issue Mar 22, 2022 · 6 comments

Comments

@sanjayupasi
Copy link

sanjayupasi commented Mar 22, 2022

Current behavior

In the following test case, firstRoute is not getting overridden by secondRoute, i.e cypress should wait for secondRoute .

Following is the reference screenshot for using cy.intercept()

Screenshot 2022-03-22 at 7 16 33 PM

Desired behavior

In the following test case, firstRoute has to be overridden by secondRoute, i.e cypress should wait for secondRoute .

Following is the reference screenshot for using cy.route()

Screenshot 2022-03-22 at 7 17 30 PM

Test code to reproduce

describe('cy.intercept', () => {
  it('> cy.route overidding', () => {
    cy.intercept('POST', '**/analytics/3.0/results').as('firstRoute')
    cy.intercept('POST', '**/analytics/3.0/results').as('secondRoute')
    cy.visit('/signup')
    cy.wait('@secondRoute')
    cy.contains('button', 'Teacher').should('be.visible')
  })
})

baseUrl: https://www.khanacademy.org/

Cypress Version

9.5.0, 9.0.0, 7.2.0, 7.0.0

Other

No response

@by-gayathri
Copy link

by-gayathri commented Mar 28, 2022

I was stuck in the same scenario and the below change worked when you want to wait for multiple occurrences of an call

describe('cy.intercept', () => {
it('> cy.route overidding', () => {
cy.intercept('POST', '**/analytics/3.0/results').as('firstRoute')
cy.visit('/signup')
cy.wait(['@firstRoute', '@firstRoute'])
cy.contains('button', 'Teacher').should('be.visible')
})
})

@sainthkh
Copy link
Contributor

Sorry for the late reply.

Actually, Cypress did wait for @secondRoute.

Peek 2022-03-28 15-42

When there are multiple routes to match, Cypress shows them as a number. You can check them out by hovering over it.

@sanjayupasi
Copy link
Author

@sainthkh

Actually, Cypress did wait for @secondRoute.

If thats the case cy.wait('firstRoute') should have been failed.

We are expecting behaviour to be same as cy.route(), as per #9302,
and we are having this problem in migrating from 4.12.1 to latest version.

In above snippet if we change cy.intercept() to cy.route(),(though we know cy.route() is depreciated in latest version),

describe('cy.intercept', () => {
  it('> cy.route overidding', () => {
    cy.server()
    cy.route('POST', '**/analytics/3.0/results').as('firstRoute')
    cy.route('POST', '**/analytics/3.0/results').as('secondRoute')
    cy.visit('/signup')
    cy.wait('@firstRoute') // cypress will error-out here
    cy.contains('button', 'Teacher').should('be.visible')
  })
})

Following is the result and it is the expected behaviour for us, same we are expecting in case of cy.intercept() as well,

Screenshot 2022-03-29 at 7 05 17 PM

@sanjayupasi
Copy link
Author

We are awaiting response...!!!
@sainthkh @jennifer-shehane @bahmutov

@sainthkh
Copy link
Contributor

I also read #9302. And the goal of the issue is to define a new way of behavior for the same route when cy.intercept() is called multiple times for the route. It has nothing to do with aliases.


In the case of this issue, the same route is intercepted multiple times with the different aliases.

"alias" means:

used to indicate that a named person is also known or more familiar under another specified name.

It's an another name of something.

The route has 2 aliases: firstRoute and secondRoute.

So, not failing when waiting for firstRoute is not a bug.


And I personally think this behavior is more intuitive to the new users because we don't usually think making a new alias deprecates the old one.


What we need now is to check if there are cases when deprecating/removing old aliases is unavoidable.

Maybe, clearIntercept in this comment might solve your problem.

@flotwig
Copy link
Contributor

flotwig commented Dec 15, 2022

As @sainthkh notes, this is working as intended. You can use the { times: 1 } parameter if you really only want to match once; otherwise, cy.intercept() will continue matching infinitely.

@flotwig flotwig closed this as not planned Won't fix, can't repro, duplicate, stale Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants