Verify contents of a JSON file using cy.readFile()
Preconditions (2)
✅ Expected Result: The test passes if all assertions on the JSON file content are true
describe('Verify JSON file contents with cy.readFile()', () => { it('should read userData.json and assert fields', () => { cy.readFile('cypress/fixtures/userData.json').then((data) => { expect(data).to.have.property('name', 'Alice') expect(data.email).to.include('@example.com') expect(data.age).to.be.a('number').and.to.be.greaterThan(18) }) }) })
This test suite reads the JSON file using cy.readFile() with the correct relative path to the fixture file.
Inside the then callback, it asserts the name property exactly matches 'Alice'.
It checks the email field contains the substring '@example.com' to verify a valid email domain.
Finally, it asserts the age is a number and greater than 18, ensuring the user is an adult.
This approach uses Cypress best practices by chaining assertions inside the promise callback and avoiding hardcoded data in the test code.
Now add data-driven testing to verify three different JSON files with different user data
cy.readFile() do in Cypress testing?cy.readFile()cy.readFile() does not docy.readFile()?contain.should('contain', 'text') is the valid syntax; others like 'equals', 'includes', or 'containText' are incorrect here.data.json contains {"name": "Alice", "age": 30}, what will this test assertion do?cy.readFile('data.json').should('deep.equal', { name: 'Alice', age: 30 })deep.equal assertioncy.readFile() parses JSON automaticallycy.readFile('log.txt').should('include', 'Error')"Name,Age,Email". Which Cypress code correctly reads the file and asserts this?contain is the correct assertion to check if the file text includes the header row.Pass the test and this pattern moves unknown