Alias created in before() only works on first test (but it's only used in the second test)
See original GitHub issueUpdate 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:
- Created 4 years ago
- Comments:7 (3 by maintainers)
@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.
@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