I’m learning cypress and I don’t understand what differs from import file from '../fixtures/filepath/file.json'
a fixture file and calling cy.fixture(file)
, and when should I use each one.
Advertisement
Answer
Basically when you say import file from '../fixtures/filepath/file.json'
you can use the imported file in any of methods in the particular javascript file. Whereas if you say cy.fixture(file.json)
, then the fixture context will remain within that cy.fixture block and you cannot access anywhere/outside of that cy.fixture block. Please go through the below code and you will understand the significance of it.
I recommend to use import file from '../fixtures/filepath/file.json'
For example. Run the below code to understand.
JavaScript
x
18
18
1
import fixtureFile from './../fixtures/userData.json';
2
describe('$ suite', () => {
3
it('Filedata prints only in cy.fixture block', () => {
4
cy.fixture('userData.json').then(fileData => {
5
cy.log(JSON.stringify(fileData)); // You can access fileData only in this block.
6
})
7
cy.log(JSON.stringify(fileData)); //This says error because you are accessing out of cypress fixture context
8
})
9
10
it('This will print file data with import', () => {
11
cy.log(JSON.stringify(fixtureFile));
12
})
13
14
it('This will also print file data with import', () => {
15
cy.log(JSON.stringify(fixtureFile));
16
})
17
});
18