Zeth
Zeth

Reputation: 2568

What's the difference between cy.readFile and cy.fixture?

What is the difference between cy.readFile and cy.fixture?

It looks like that they both do pretty much the same thing: Yields the content of a file. Except that cy.fixture( ... ) looks inside the fixture-folder and cy.readFile( ... ) looks from the project root.


And if both could be used, are there then pro's/con's by using one of the two?

Upvotes: 2

Views: 236

Answers (1)

Etheryte
Etheryte

Reputation: 25310

The main difference is conceptual, but there are some practical considerations as well.

Fixtures are meant for files that are used only for your tests, e.g. placeholder test data, sample responses and so forth. In other terms, fixtures are files that would not be a part of your project if you didn't have tests.

For this end, fixtures provide some convenience utilities, such as piping them directly into cy.route():

cy.route('GET', '/users', 'fixture:users');

readFile() on the other hand is a generic method that you can use to read more or less anything. It is a good candidate if you need to read files that are a part of your regular project code, e.g. translations:

cy.readFile('translations/en_US.json').then(...);

Upvotes: 3

Related Questions