question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Waiting for request with cy.wait when alias name matches tag name breakes cy.get command (interception becomes subject)

See original GitHub issue

Current behavior

I created repository with reproduction https://github.com/shwarcu/cypress-alias-bug-repro

When few conditions are met, cy.get command is returning wrong subject (intercepted request instead of DOM element). It behaves like cy.get('@aliasname') instead looking for an actual element. Here are conditions:

  • alias has the same name as element we lookup (in reproduction I used video alias name and element)
  • request is intercepted
  • request is awaited with cy.wait

Example test that fails

  it('fails when using get command after previously waiting for alias with the same name', () => {
    cy.intercept(DEFAULT_VIDEO_URL, (request) => {
      request.redirect(OTHER_VIDEO_URL);
    }).as('video');
    cy.visit('');

    cy.wait('@video');


    cy.get('video').should('have.attr', 'src'); // here subject is not video element, but interception object
  });
Screenshot 2022-11-19 at 15 25 07

Desired behavior

cy.get('video') should not behave like cy.get('@video'). It should yield video element.

Test code to reproduce

https://github.com/shwarcu/cypress-alias-bug-repro

Cypress Version

11.1.0

Node version

v16.13.0

Operating System

MacOS 13.0.1 / Manjaro GNOME 43.1

Debug Logs

No response

Other

No response

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
BlueWindscommented, Dec 5, 2022

Interesting, and very unusual - thank you for the repro!

This appears to be fixed in Cypress 12.0.0, which should be coming out later this week. You can try it out with the pre-release binaries if you like (https://github.com/cypress-io/cypress/commit/2552bdda9c8d8a30fa4c570f561f619ccb1cb8b3 is the latest as of this writing).

There appears to be some additional conditions to trigger the bug. This test:

  it.only('gets confused', () => {
    cy.intercept(/users/, {}).as('div')

    cy.visit('')
    cy.window().then((win) => {
      return win.Cypress.$.get('/users')
    })

    cy.wait('@div')

    cy.get('div').should('have.attr', 'id')
  })

passes in Cy 11.1.0 - .get('div') is working as expected. I’m not going to dive too deep into it, since the issue seems to be resolved in Cy12 already.

I’m going to close this, but please feel free to ping me or comment here again if you’re still seeing issues in Cy12. I’m the one that rewrote our aliasing logic recently, so if there are problems I always want to hear about them!

1reaction
ryanthemanuelcommented, Nov 22, 2022

Apologies @shwarcu. I think I completely skipped over your link to your reproduction and consequently was trying to set up something myself. I can confirm that your reproduction at https://github.com/shwarcu/cypress-alias-bug-repro/blob/main/cypress/e2e/example.spec.cy.ts reproduces this issue and I will route this to the e2e team for prioritization.

Read more comments on GitHub >

github_iconTop Results From Across the Web

wait - Cypress Documentation
When used with an alias, cy.wait() goes through two separate "waiting" periods. The first period waits for a matching request to leave the...
Read more >
Waiting for request with cy.wait when alias name matches tag ...
Waiting for request with cy.wait when alias name matches tag name breakes cy.get command (interception becomes subject) #1491. Sign in to view logs....
Read more >
Cypress cy.intercept Problems - Gleb Bahmutov
The command cy.intercept can match requests using a substring, ... In that case the first cy.wait(alias) "uses up" the intercept's response.
Read more >
How to wait for a request to finish before moving on with Cypress
In the first line inside of the beforeEach function callback, I use cy.intercept() to intercept an HTTP request of type GET for a...
Read more >
Cypress - The Blue Book - GitHub Pages
Cypress automatically waits for commands and assertions before moving on. ... the subject yielded from the cy.get() command, which will be a DOM...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found