Rishabh Raghwendra
Rishabh Raghwendra

Reputation: 320

Getting (void 0) is undefined error in cypress

I have this test case:

before(()=>{
    cy.fixture('example').then(function (data) {
      this.data = data;
    })
    cy.visit("http://127.0.0.1:3000/");
    cy.contains("Connect Wallet").click();
    cy.contains("Metamask").click();
    cy.wait(10000)
    cy.contains("0x352e").click();
    cy.contains("Profile").click();
    cy.wait(10000); // waiting for the profile to completely load
  })

 it.only("Edit User Profile",()=>{
    cy.contains("Edit Profile").click();
    cy.get('[data-testid="name"]').click();
    cy.get('[data-testid="name"]').type(this.data.name);  // getting error on this line
  })

I am getting error (void 0) is undefined . What am I doing wrong ? Do I need to import any module to use the fixture's data ?

Upvotes: 2

Views: 671

Answers (1)

Alapan Das
Alapan Das

Reputation: 18634

If you are using the this context, you have to use function. From cypress docs:

If you store and access the fixture data using this test context object, make sure to use function () { ... } callbacks. Otherwise the test engine will NOT have this pointing at the test context.

it.only('Edit User Profile', function () {
  cy.contains('Edit Profile').click()
  cy.get('[data-testid="name"]').click()
  cy.get('[data-testid="name"]').type(this.data.name)
})

Upvotes: 3

Related Questions