Challenge - 5 Problems
cy.readFile() Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate2: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? });
Attempts:
2 left
💡 Hint
Check the exact value of the key, not its type or length.
✗ Incorrect
The key 'status' should have the string value 'success'. Option B checks for exact equality. Option B wrongly expects a boolean true. Option B expects undefined which is incorrect. Option B expects a length which is invalid for a string value check.
❓ Predict Output
intermediate1: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')
Attempts:
2 left
💡 Hint
The assertion checks if the file content includes the substring.
✗ Incorrect
The 'should' assertion with 'contain' checks if the string includes the given substring. Since 'World' is in 'Hello World', the test passes. The file being text is fine for this assertion.
🔧 Debug
advanced2: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 })
Attempts:
2 left
💡 Hint
Check the data type of the value in the JSON file.
✗ Incorrect
If the JSON file has "enabled": "true" (string), the assertion expecting boolean true fails. The assertion syntax and file reading are correct. The file path is assumed correct.
🧠 Conceptual
advanced3: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?Attempts:
2 left
💡 Hint
Use Cypress chaining to access nested properties efficiently.
✗ Incorrect
Option C uses
its() to directly access the 'version' key, which is efficient and readable. Option C reads the whole file and then asserts, which is less efficient. Option C checks for a string pattern which is brittle. Option C is not supported by cy.readFile.❓ framework
expert3: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?Attempts:
2 left
💡 Hint
Use built-in Cypress assertions that support automatic retry.
✗ Incorrect
Option D uses 'should' with 'have.property' which Cypress retries automatically until the condition is met or timeout. Option D uses a callback but may not retry properly. Option D throws error manually but does not leverage Cypress retry. Option D expects exact object equality which may fail if other keys exist.