Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Q: Cypress fixtures - cannot read property of undefined

Tags:

cypress

I'm trying to use fixtures to hold data for different tests. This is an example of the code. When it gets to the second test I'm getting 'Cannot read property 'email' of undefined'.

Any ideas why and how I can get around that? I'm new to this and followed a course where they said the whole point of using fixtures in the 'before' was to have the data accessible to everything. Is that wrong?

Thank you

describe('Example', function() {
  before(function() {
    cy.fixture('dataFile').then(function(dataJson) {
      this.dataJson = dataJson;
    });
  });

  it('name', function() {
    cy.log(this.dataJson.email);
  });
  it('name2', function() {
    cy.log(this.dataJson.email);
  });
});
like image 681
Cos Avatar asked Oct 27 '25 03:10

Cos


1 Answers

You can use the below solution to make it work:

describe('Example', function() {
  let testData;

  before(function() {
    cy.fixture('dataFile').then(function(dataJson) {
      testData = dataJson;
      return testData;
    });
  });

  it('name', function() {
    cy.log(testData.email);
  });
  it('name2', function() {
    cy.log(testData.email);
  });
});
like image 199
Tahera Firdose Avatar answered Oct 29 '25 19:10

Tahera Firdose