Fixture in cypress

I'm trying to connect the fixture like this, use beforeEach cy.fixture('tabNetwork.json').then(data => {this.data = data}) but Cypress gives me an error(Cannot set properties of undefined (setting 'data') Because this error occurred during a before each hook we are skipping the remaining tests in the current suite: ) , I don't understand what's wrong I always do it, but now something wrong. How to fix this?

Upvotes: 0

Views: 48

Answers (1)

Freek.Terling
Freek.Terling

Reputation: 150

You cannot use an arrow function if you want to set a property of this.

In a Cypress test you must use a standard function, like so

.then(function(data) {this.data = data})

See Sharing Context

Or at the test definition level is fine, since arrow functions inherit parent this scope

it('test that uses a fixture', function() {

  cy.fixture('tabNetwork.json').then(data => {this.data = data})

See Scope difference between normal functions and arrow functions


Alternatively use require to read fixtures at the top of the spec

const data = require('../fixtures/tabNetwork.json');  // adjust relative path


Upvotes: 5

Related Questions