Home > Net >  Cypress V10. Not Reading data in BDD test: TypeError: Cannot read properties of undefined (reading &
Cypress V10. Not Reading data in BDD test: TypeError: Cannot read properties of undefined (reading &

Time:10-08

I hope someone can help, I've just converted a Cypress Mocha framework to BDD. Before converting it was running perfectly and the test was running smoothly. Now I've converted it I seem to be getting an error message Cannot read properties of undefined (reading 'mobileHandset'). I never had this issue before so I'm very confused. here is the code enter image description here

CodePudding user response:

You don't need to import the data fixture if you already have it in the cypress/fixtures folder.

You can load the fixture in the Before hook before your tests.

import { 
    Given,
    And,
    Then,
    When,
    Before 
} from "@badeball/cypress-cucumber-preprocessor";
//...

Before(function() {
    cy.fixture('example').then((data) => {
        this.data = data;
    });
});
//...

CodePudding user response:

Your beforeEach() should be working, but it's not necessary you can just refer to data instead of this.data.

const data = require ('../../../fixtures/example.json');  // data available anywhere in this step
...
When(/^I add items to cart$/, () => {
  ...
  data.mobileHandset.forEach(element => {
    cy.AddToCart(element)  
  })
  ...
})

The convention is to use cy.fixture()

const data = require ('../../../fixtures/example.json');  // data available anywhere in this step
...
When(/^I add items to cart$/, () => {
  ...
  cy.fixture('example.json').then(data => {    // no ../.. needed
    data.mobileHandset.forEach(element => {
      cy.AddToCart(element)  
    })
  }) 
  ...
});
  • Related