FarFarAway
FarFarAway

Reputation: 1115

Reading fixture using typescript return undefined in other steps

I want to read data from json file and use it in my typescript test file. therefor i have created a beforeeach

describe(`test`, () => {
  beforeEach(() => {
    cy.fixture("testdata").then(function (data: any) {
      cy.log(`reading fixture file`);
      this.data = data;
      cy.log(data.firstName);
      this.firstname = data.firstName;
    });
  });
  it(`testdata should be read`, () => {
    // the value of firstname is undefined at this point
  });
});

I do not understand why and how i could read the data correctly.

Upvotes: 0

Views: 537

Answers (1)

user15239601
user15239601

Reputation:

Please use function() { instead of () => { when referencing properties of this.

Examples shown here Sharing Context, but it's not just limited to aliases - any reference to this needs to follow this rule (both the beforeEach() callback and the it() callback).

Upvotes: 1

Related Questions