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.

Alias created in before() only works on first test (but it's only used in the second test)

See original GitHub issue

Update 10/16: See https://github.com/cypress-io/cypress/issues/5351#issuecomment-542779224 for a clearer example and explanation.

Original Post Below This Line

------------------------------------------

This appears to be really similar to https://github.com/cypress-io/cypress/issues/2766 (and https://github.com/cypress-io/cypress/issues/665 in turn), but it seems like it’s a bit different. The alias definition exists, like user as an object exists, and user.id exists, but the value of user.id is null.

Note that an alias created within the second test doesn’t appear to work –

it('does something', function () {
  cy.createUser('xyz').as('user') // createUser is an exec() outside of the application
  this.user.something... 
    // expected this to be available, 
    // as this works if I stuck it into a beforeEach 
    // on any test, or a before on the first test
})

Current behavior:

The before() hook contains an alias. I would expect this to have this.user available as an object. If it is in the first test (I copy and move the test to the top), the test works. However, after the first test, this.user exists, but its value is empty and undefined.

Desired behavior:

I would expect this.user to remain a consistent object available throughout each test in the spec file. I can currently get around this with cy.createUser().then((user) => ), but that’s why I wanted to use aliases in the first place. Alternatively, I’d expect an alias to be create-able within a test.

Steps to reproduce:

Test setup:

before(function() {
  cy.createUser('foo').as('user') // returns an object that has a value called user.id
})

it('is the first test') {
 // nothing happens here
}

it('is a second test') {
  cy.visit(`/users/${this.user.id}`)
}

Versions

Latest cypress (Oct 11 2019), chrome, fedora.

If you need more info, please feel free to ask.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
dsesamicommented, Oct 25, 2019

@jennifer-shehane as this is apparently a very regularly-duplicated issue, may I suggest that the docs be updated to specify this nuance? Many users get tripped up by it.

0reactions
jennifer-shehanecommented, Jan 3, 2020

@dsesami We updated the scope of the original issue to issue a warning when defining an alias in a before, please direct all comments to the original issue. https://github.com/cypress-io/cypress/issues/665#issuecomment-552961024

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress Create the Aliases before each test - ProgramsBuzz
In this article, we will discuss how we can create an alias in before each hook and access them in the test steps....
Read more >
cypress - Setting a variable in before cannot be accessed in a ...
A common user mistake is to create aliases using the before hook. Such aliases work in the first test only! But there's a...
Read more >
Variables and Aliases - Cypress Documentation
Note: all aliases are reset before each test. A common user mistake is to create aliases using the before hook. Such aliases work...
Read more >
Use Cypress Aliases Set Inside The beforeEach and before ...
We can conveniently avoid the pyramid of Doom of . then callbacks by using "beforeEach" hook to fetch our data. We can also...
Read more >
14415 (Multiple aliases for one database: testing problems)
In these cases, the test framework just treats the two aliases as separate databases; which means it first creates the test database for...
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