0
0
Cypresstesting~20 mins

cy.readFile() assertions in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
cy.readFile() Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
assertion
intermediate
2:00remaining
Correct assertion for JSON file content
You use cy.readFile('data.json') to read a JSON file. Which assertion correctly checks that the file contains a key status with value success?
Cypress
cy.readFile('data.json').then((data) => {
  // Which assertion is correct here?
});
Aexpect(data['status']).to.be.true
Bexpect(data.status).to.equal('success')
Cexpect(data.status).to.be.undefined
Dexpect(data.status).to.have.length(7)
Attempts:
2 left
💡 Hint
Check the exact value of the key, not its type or length.
Predict Output
intermediate
1:30remaining
Output of reading a text file and asserting content
Given a text file message.txt containing the text Hello World, what will be the result of this test code?
Cypress
cy.readFile('message.txt').should('contain', 'World')
ATest passes because 'World' is part of the file content
BTest fails because 'contain' is not a valid assertion
CTest fails because 'Hello' is missing
DTest throws an error because file is not JSON
Attempts:
2 left
💡 Hint
The assertion checks if the file content includes the substring.
🔧 Debug
advanced
2:30remaining
Why does this cy.readFile() assertion fail?
You run this code but the test fails. Why?
Cypress
cy.readFile('config.json').should((data) => {
  expect(data.enabled).to.be.true
})
AThe key 'enabled' is a string 'true', not boolean true
Bcy.readFile cannot read JSON files
CThe assertion syntax is incorrect
DThe file path is wrong
Attempts:
2 left
💡 Hint
Check the data type of the value in the JSON file.
🧠 Conceptual
advanced
3:00remaining
Best practice for asserting large JSON file content
You read a large JSON file with cy.readFile('large.json'). Which approach is best to assert that the file contains a key version with value 1.2.3 without loading the entire file into memory?
AUse <code>cy.readFile('large.json').should('contain', '"version":"1.2.3"')</code>
BRead the whole file and use <code>expect(data.version).to.equal('1.2.3')</code>
CUse <code>cy.readFile('large.json').its('version').should('equal', '1.2.3')</code>
DManually parse the file line by line with <code>cy.readFile</code>
Attempts:
2 left
💡 Hint
Use Cypress chaining to access nested properties efficiently.
framework
expert
3:00remaining
Handling asynchronous assertion failures with cy.readFile()
You want to retry reading a file until it contains a key ready set to true. Which Cypress code snippet correctly implements this retry logic?
Acy.readFile('status.json').should((data) => { expect(data.ready).to.be.true })
Bcy.readFile('status.json').then((data) => { if (!data.ready) throw new Error('Not ready') })
Ccy.readFile('status.json').should('equal', { ready: true })
Dcy.readFile('status.json').should('have.property', 'ready', true)
Attempts:
2 left
💡 Hint
Use built-in Cypress assertions that support automatic retry.