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.

intercept leaks between tests when combined with aliases

See original GitHub issue

Current behavior

Using aliases in combination with intercept causes tests to interfere with other tests regularly.

Desired behavior

Intercept is cleaned up properly between tests.

Test code to reproduce

The below test will reproduce this. For me it reproduces it about 60% of the time. I can reproduce it 100% of the time in the closed source test where I encountered the issue, perhaps because it has a lot more assertions. I suppose it may depend on your hardware.

It seems that the error disappears if I clear cookies in afterEach.

const url = 'https://www.cypress.io/'
const dummyRequest = () =>
  cy.request({
    url,
  })

context('InterceptBug', () => {
  beforeEach(() => {
    dummyRequest().as('home')
    cy.visit(url)
  })
  // afterEach(() => {
  //   cy.clearCookies()
  // })

  it('shows the logo', () => {
    cy.get('[alt="Cypress.io"]').should('exist')
  })
  const navigate = () => {
    cy.get('[alt="Cypress.io"]').should('exist')

    // The second time this is called, this sometimes triggers an additional
    // error: `Cannot read property 'fireChangeEvent' of undefined`
    // Other times it will fail on the previous selector with the (assert)
    // error: `expected undefined to exist`
    // I figured those are different bugs, so consider them out of scope for
    // this issue.
    cy.intercept(url).as('navigation')

    cy.contains('Features').click({
      force: true,
    })

    cy.wait('@navigation')

    // If the below visit is removed the test should succeed most of the time
    cy.visit(url)
  }
  it('allows navigating', navigate)
  it('allows navigating2 very irregularly fails', navigate)
  const useHomeAlias = () => {
    cy.get<string>('@home').then((response) => {
      // do something with response
      dummyRequest()
    })
  }
  // cy.get() could not find a registered alias for: @home.
  // You have not aliased anything yet.
  it('use home alias fails', useHomeAlias)
  it('use home alias succeeds', useHomeAlias)
})

export {}

Versions

Cypress version 6.0.0, N/A, Chrome 87, macOS Catalina, N/A

Possibly related issues:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
jennifer-shehanecommented, Nov 30, 2020

I can’t always get the tests to fail, but it pretty reliably fails with the example below. I could never get the allows navigating2 very irregularly fails test to fail, so if you have a simpler example of this, please provide it or open a new issue.

I’m not totally sure this is related to intercept exactly, it seems like a combination of things are involved.

Reproducible Example

The ‘use home alias fails’ test doesn’t seem to be running the beforeEach hook.

context('InterceptBug', () => {
  beforeEach(() => {
    cy.request({
      url: 'https://www.cypress.io/',
    }).as('home')
    cy.visit('https://www.cypress.io/')
  })

  it('allows navigating', () => {
    cy.get('[alt="Cypress.io"]')
    cy.intercept('https://www.cypress.io/')
  })

  // cy.get() could not find a registered alias for: @home.
  // You have not aliased anything yet.
  it('use home alias fails', () => {
    cy.get('@home')
  })

  it('use home alias succeeds', () => {
    cy.get('@home')
  })
})
Screen Shot 2020-11-30 at 10 07 00 AM
2reactions
jennifer-shehanecommented, Jan 15, 2021

This has been resolved and is no longer present in our most recent version - 6.2.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress cy.intercept Problems - Gleb Bahmutov
Unfortunately overwriting cy. intercept has a bug with aliases #9580, and thus we cannot always show when the intercept is registered. We can ......
Read more >
How to access alias in different test? - cypress - Stack Overflow
Show activity on this post. I have an alias defined in 1 test and I would like to use the result in a...
Read more >
Best Practices - Cypress Documentation
Anti-Pattern: Coupling multiple tests together. Best Practice: Tests should always be able to be run independently from one another and still pass ....
Read more >
Cypress Aliases Intercepted Routes - ProgramsBuzz
We can use this fake response and use that particular JSON data to do tests afterward. Once the actual API is created we...
Read more >
Spectral leakage - Wikipedia
... produces leakage, which we call aliases of the original spectral component. For Fourier transform purposes, sampling is modeled as a product between...
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