Bird
0
0

You want to verify that a text file notes.txt contains the word "important" anywhere in its content using Cypress. Which code snippet correctly performs this check?

hard📝 Application Q15 of 15
Cypress - File Operations
You want to verify that a text file notes.txt contains the word "important" anywhere in its content using Cypress. Which code snippet correctly performs this check?
Acy.readFile('notes.txt').then((text) => { expect(text.includes('important')).to.be.true; })
Bcy.readFile('notes.txt').should('contain', 'important')
Ccy.readFile('notes.txt').then((text) => { expect(text).to.equal('important'); })
Dcy.readFile('notes.txt').then((text) => { expect(text).to.have.property('important'); })
Step-by-Step Solution
Solution:
  1. Step 1: Understand file content check

    We want to check if the word "important" is anywhere in the text, so we need to check substring presence.
  2. Step 2: Evaluate options

    cy.readFile('notes.txt').then((text) => { expect(text.includes('important')).to.be.true; }) uses includes() and asserts true, which is correct. cy.readFile('notes.txt').should('contain', 'important') is invalid because should('contain') is for DOM elements, not file content. cy.readFile('notes.txt').then((text) => { expect(text).to.equal('important'); }) checks exact equality, which is wrong. cy.readFile('notes.txt').then((text) => { expect(text).to.have.property('important'); }) wrongly uses to.have.property() on a string.
  3. Final Answer:

    cy.readFile('notes.txt').then((text) => { expect(text.includes('important')).to.be.true; }) -> Option A
  4. Quick Check:

    Use includes() to check substring in file text [OK]
Quick Trick: Use includes() inside then() to check text content [OK]
Common Mistakes:
  • Using should('contain') on file content
  • Checking exact equality instead of substring
  • Using property assertion on string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes