I have a cy.fixture('file.json').as('fixtureAlias') call in a beforeEach before my test but when the test starts and I call this.fixtureAlias, I'm told "TypeError: Cannot read property 'fixtureAlias' of undefined.
Be able to use my aliased fixture file.
describe('State Checker', function () {
beforeEach(function () {
cy.fixture('file.json').as('fixtureAlias')
})
it('Gets file to do stuff with', function () {
var fileContents = this.fixtureAlias
})
})
cypress 3.4.1, ubuntu 16.04, Chrome 77
Thanks!
I ran the exact code provided, using my own fixture file which exists and is valid JSON - there is no error given in version 3.4.1 or 3.5.0.
describe('State Checker', function () {
beforeEach(function () {
cy.fixture('example.json').as('fixtureAlias')
})
it('Gets file to do stuff with', function () {
let fileContents = this.fixtureAlias
})
})
Unfortunately we have to close this issue as there is not enough information to reproduce the problem. This does not mean that your issue is not happening - it just means that we do not have a path to move forward.
Please comment in this issue with a reproducible example and we will reopen the issue. 馃檹
This still isn't working for me so I'm back with more info. I've tried a couple of way to do this and neither of them work, to different degrees. My fixture file exists and is valid JSON.
describe('Affix file beforeEach as alias', function () {
beforeEach(function() {
cy.fixture('logins.json').as('logins')
console.log(this.logins)
})
it('Tries to use logins', function () {
console.log(this.logins.length)
})
})
The result of running this test is the beforeEach's console.log outputs 'undefined' and the test's console.log makes Cypress say 'TypeError: Cannot read property 'logins' of undefined'.
I've also tried setting it like so:
describe('Affix file beforeEach with hook', function () {
beforeEach(function() {
cy.fixture('logins.json').then(logins => {
this.logins = logins
console.log(this.logins.length)
})
})
it('Tries to use logins', function () {
console.log(this.logins.length)
})
})
The result of this one is the beforeEach's console.log outputs '3' (which is the number of logins in logins.json) and the test's console.log also makes Cypress say 'TypeError: Cannot read property 'logins' of undefined'.
I'm on Cypress 3.6.1, Ubuntu 16.04, and Chrome 78. Please let me know if you need any additional info, I'm very interested in getting this to work.
Thanks!
@mlyonsFE same as @jennifer-shehane, cannot reproduce either of your problems (running 3.6.1, stable & electron).
As for why the following logs undefined:
beforeEach(function() {
cy.fixture('logins.json').as('logins')
console.log(this.logins) // <---------------
})
Cypress commands are async, thus you can't access the mocha context synchronously.
If you need more help, please supply a reproducible test case by forking https://github.com/cypress-io/cypress-test-tiny.
I had the same problem. I didn't mind that this.* isn't working inside arrow functions: https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Avoiding-the-use-of-this
Most helpful comment
I had the same problem. I didn't mind that
this.*isn't working inside arrow functions: https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Avoiding-the-use-of-this