Bird
Raised Fist0
Cypresstesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does cy.readFile() do in Cypress testing?
easy
A. Writes data to a file during the test
B. Reads the content of a file so you can check its data
C. Deletes a file from the system
D. Uploads a file to a server

Solution

  1. Step 1: Understand the purpose of cy.readFile()

    This command is used to read the contents of a file during a Cypress test.
  2. Step 2: Identify what cy.readFile() does not do

    It does not write, delete, or upload files; it only reads them.
  3. Final Answer:

    Reads the content of a file so you can check its data -> Option B
  4. Quick Check:

    cy.readFile() = Read file content [OK]
Hint: Remember: readFile reads, not writes or deletes [OK]
Common Mistakes:
  • Confusing readFile with writeFile
  • Thinking it uploads files
  • Assuming it deletes files
2. Which of the following is the correct syntax to assert that a file contains the text "Hello World" using cy.readFile()?
easy
A. cy.readFile('file.txt').should('containText', 'Hello World')
B. cy.readFile('file.txt').should('equals', 'Hello World')
C. cy.readFile('file.txt').should('includes', 'Hello World')
D. cy.readFile('file.txt').should('contain', 'Hello World')

Solution

  1. Step 1: Identify the correct assertion method for substring check

    The correct assertion to check if a string contains text is contain.
  2. Step 2: Match syntax with Cypress documentation

    should('contain', 'text') is the valid syntax; others like 'equals', 'includes', or 'containText' are incorrect here.
  3. Final Answer:

    cy.readFile('file.txt').should('contain', 'Hello World') -> Option D
  4. Quick Check:

    Use 'contain' for substring assertions [OK]
Hint: Use 'contain' to check if file text includes a string [OK]
Common Mistakes:
  • Using 'equals' instead of 'contain' for substring
  • Using 'includes' which is not a Cypress assertion
  • Using 'containText' which is for DOM elements
3. Given the file data.json contains {"name": "Alice", "age": 30}, what will this test assertion do?
cy.readFile('data.json').should('deep.equal', { name: 'Alice', age: 30 })
medium
A. Pass because the file content matches the object exactly
B. Fail because the object keys are strings in the file
C. Fail because 'deep.equal' cannot compare objects
D. Pass but only if the file is a string, not JSON

Solution

  1. Step 1: Understand deep.equal assertion

    This assertion checks if two objects have the same structure and values deeply.
  2. Step 2: Check if cy.readFile() parses JSON automatically

    Yes, it parses JSON files into objects, so the assertion compares objects correctly.
  3. Final Answer:

    Pass because the file content matches the object exactly -> Option A
  4. Quick Check:

    deep.equal compares objects deeply [OK]
Hint: Use deep.equal to compare JSON objects, not strings [OK]
Common Mistakes:
  • Thinking file content is a string, not parsed JSON
  • Confusing deep.equal with shallow equal
  • Assuming keys must be non-string
4. What is wrong with this Cypress test code?
cy.readFile('log.txt').should('include', 'Error')
medium
A. The file must be JSON to use should assertions
B. The file path must be absolute, not relative
C. The assertion method should be 'contain', not 'include'
D. The method 'include' is correct and will pass if 'Error' is found

Solution

  1. Step 1: Check the assertion method name

    Cypress uses 'contain' for substring assertions; 'include' is not a valid assertion method.
  2. Step 2: Confirm correct assertion for substring

    The correct method is 'contain' to check if text includes a substring.
  3. Final Answer:

    The assertion method should be 'contain', not 'include' -> Option C
  4. Quick Check:

    Use 'contain' for substring assertions, not 'include' [OK]
Hint: Use 'contain' for substring checks, not 'include' [OK]
Common Mistakes:
  • Using 'include' instead of 'contain'
  • Thinking relative paths are invalid
  • Assuming only JSON files can be read
5. You want to verify a downloaded CSV file contains the header row exactly as "Name,Age,Email". Which Cypress code correctly reads the file and asserts this?
hard
A. cy.readFile('downloads/data.csv').should('contain', 'Name,Age,Email')
B. cy.readFile('downloads/data.csv').should('equal', 'Name,Age,Email')
C. cy.readFile('downloads/data.csv').should('deep.equal', 'Name,Age,Email')
D. cy.readFile('downloads/data.csv').should('include', 'Name,Age,Email')

Solution

  1. Step 1: Understand the file content and assertion needed

    The CSV file is text, and we want to check if it contains the exact header row as a substring.
  2. Step 2: Choose the correct assertion for substring in text files

    contain is the correct assertion to check if the file text includes the header row.
  3. Final Answer:

    cy.readFile('downloads/data.csv').should('contain', 'Name,Age,Email') -> Option A
  4. Quick Check:

    Use 'contain' to check text includes header row [OK]
Hint: Use 'contain' to check CSV header text inside file [OK]
Common Mistakes:
  • Using 'equal' which requires exact full file match
  • Using 'deep.equal' which is for objects, not strings
  • Using 'include' which is not a valid assertion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard