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

Alias in before block disappears after first test #18161

Closed
ashleymercer opened this issue Sep 20, 2021 · 2 comments
Closed

Alias in before block disappears after first test #18161

ashleymercer opened this issue Sep 20, 2021 · 2 comments

Comments

@ashleymercer
Copy link

ashleymercer commented Sep 20, 2021

Current behavior

If we alias a value in a before() block then it can only be retrieved (with cy.get()) in the first test in a suite; attempting to retrieve it in the second or subsequent tests results in

cy.get() could not find a registered alias for: @alias.
You have not aliased anything yet.

Desired behavior

It is quite common to want to run a some initial query (e.g. a fetch from an API) at the start of a suite of tests, and alias with cy.as(). This alias should be available to all subsequent tests (within the current scope / context).

Test code to reproduce

describe(`Test`, () => {
  
  function fetchUsers() {
    return new Promise<string[]>(resolve => resolve(['Alice', 'Bob']))
  }

  before(() => {
    cy.wrap(fetchUsers()).as('users')
  })

  // This test passes...
  it(`test 1`, () => {
    cy.get('@users').should('have.length', 2)
  })

  // ...but this one fails
  it(`test 2`, () => {
    cy.get('@users').should('have.length', 2)
  })
})

Cypress Version

8.3.1

Other

One workaround is to move the setup steps to beforeEach but this might be heavyweight (e.g. running a bunch of initial API requests on every single test, rather than once per suite)

@ashleymercer
Copy link
Author

I suspect this bug has the same root cause as #17467 but that issue was closed without being resolved, so this is still an issue in the latest versions.

@jennifer-shehane
Copy link
Member

Duplicate of #665 Aliases can't be used in before blocks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants