Jan
Jan

Reputation: 146

Loading fixture with component test

Performing a Cypress component test, I load a fixture in a before hook.

The fixture loads, but disappears after the first test.

before(() => {
  cy.fixture('my-fixture').as('fixture')
})

it('test1', function() {
  mount(component, {
    propsData: {'data': this.fixture},
  })
})

it('test2', function() {
  mount(component, {
    propsData: {'data': this.fixture},  // undefined
  })
})

How do I fix the fixture?

Upvotes: 3

Views: 811

Answers (1)

Fody
Fody

Reputation: 32138

Alises are cleared down between tests. You can change the hook to beforeEach() to allow all tests to see the data.

Note that the fixture is not read from disk every time it's called, the cy.fixture() command has a cache the returns the previously read value on 2nd, 3rd calls.

beforeEach(() => {
  cy.fixture('my-fixture').as('fixture')
})

it('test1', function() {
  mount(component, {
    propsData: {'data': this.fixture},
  })
})

it('test2', function() {
  mount(component, {
    propsData: {'data': this.fixture},  // passes
  })
})

Upvotes: 4

Related Questions