Bird
Raised Fist0
Cypresstesting~20 mins

Writing to files (cy.writeFile) in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
File Writer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Cypress test writing JSON to a file?
Consider the following Cypress test code that writes a JSON object to a file. What will be the content of the file output.json after this test runs?
Cypress
cy.writeFile('output.json', { name: 'Alice', age: 30 })
A
{
  "name": "Alice",
  "age": 30
}
B"{ name: 'Alice', age: 30 }"
C{ name: 'Alice', age: 30 }
D["name", "Alice", "age", 30]
Attempts:
2 left
💡 Hint
cy.writeFile serializes objects to JSON format by default.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies the file content after writing?
After running cy.writeFile('data.txt', 'Hello World'), which Cypress assertion correctly checks that the file contains exactly 'Hello World'?
Acy.readFile('data.txt').should('equal', 'Hello World')
Bcy.readFile('data.txt').should('contain', 'Hello World!')
Ccy.readFile('data.txt').should('include', 'hello world')
Dcy.readFile('data.txt').should('eq', 'Hello World')
Attempts:
2 left
💡 Hint
Use the exact string match assertion.
🔧 Debug
advanced
2:00remaining
Why does this cy.writeFile command fail with a TypeError?
Examine the code below. Why does it cause a TypeError during test execution?
const data = new Map([['key', 'value']]);
cy.writeFile('map.json', data);
Cypress
const data = new Map([['key', 'value']]);
cy.writeFile('map.json', data);
Acy.writeFile requires a string, but data is a Map, causing a TypeError.
BThe file path 'map.json' is invalid and causes a TypeError.
CMap objects cannot be serialized to JSON directly, causing a TypeError.
DThe Map object is empty, so cy.writeFile throws a TypeError.
Attempts:
2 left
💡 Hint
Think about how JavaScript objects are converted to JSON.
🧠 Conceptual
advanced
2:00remaining
What is the best practice for writing large JSON data to a file in Cypress?
You have a large JSON object to write to a file during a Cypress test. Which approach is best to ensure the test runs efficiently and the file is correctly written?
AUse cy.writeFile with the JSON object directly; Cypress handles serialization efficiently.
BConvert the JSON object to a string with JSON.stringify and then use cy.writeFile with the string.
CSplit the JSON object into smaller parts and write multiple files with cy.writeFile.
DWrite the JSON object to a file using Node.js fs module inside the test.
Attempts:
2 left
💡 Hint
Consider Cypress's built-in handling of objects in cy.writeFile.
❓ framework
expert
2:00remaining
How to ensure cy.writeFile completes before next test step in Cypress?
In a Cypress test, you write data to a file using cy.writeFile. You want to make sure the file write finishes before continuing to the next command. Which code snippet correctly ensures this?
Acy.writeFile('file.txt', 'data'); cy.log('Write complete')
Bcy.writeFile('file.txt', 'data').then(() => { cy.log('Write complete') })
Cawait cy.writeFile('file.txt', 'data'); cy.log('Write complete')
Dcy.writeFile('file.txt', 'data').catch(() => { cy.log('Write failed') })
Attempts:
2 left
💡 Hint
Use Cypress command chaining to wait for completion.

Practice

(1/5)
1. What is the main purpose of cy.writeFile() in Cypress testing?
easy
A. To delete a file after a test
B. To read data from a file during a test
C. To save data to a file during a test
D. To open a file in the browser

Solution

  1. Step 1: Understand the function's role

    cy.writeFile() is used to save or write data into a file during Cypress tests.
  2. Step 2: Differentiate from similar functions

    cy.readFile() reads data, but cy.writeFile() writes data. Deleting or opening files is not its purpose.
  3. Final Answer:

    To save data to a file during a test -> Option C
  4. Quick Check:

    Write data = A [OK]
Hint: Remember: writeFile writes, readFile reads [OK]
Common Mistakes:
  • Confusing writeFile with readFile
  • Thinking it deletes files
  • Assuming it opens files in browser
2. Which of the following is the correct syntax to write the string 'Hello World' to a file named 'greeting.txt' using cy.writeFile()?
easy
A. cy.writeFile('greeting.txt', {text: 'Hello World'})
B. cy.writeFile('greeting.txt', 'Hello World')
C. cy.writeFile('greeting.txt' => 'Hello World')
D. cy.writeFile('greeting.txt', ['Hello World'])

Solution

  1. Step 1: Identify correct parameter usage

    cy.writeFile() takes the file path as first argument and the content as second argument, which can be a string.
  2. Step 2: Check syntax correctness

    cy.writeFile('greeting.txt', 'Hello World') uses correct syntax: file path string and content string separated by a comma.
  3. Final Answer:

    cy.writeFile('greeting.txt', 'Hello World') -> Option B
  4. Quick Check:

    Correct syntax uses comma between path and content [OK]
Hint: Use comma between file path and content [OK]
Common Mistakes:
  • Using arrow (=>) instead of comma
  • Passing content inside an object unnecessarily
  • Passing content as an array instead of string
3. Consider the following Cypress code snippet:
cy.writeFile('data.json', { name: 'Alice', age: 30 })
cy.readFile('data.json').then((content) => {
  console.log(content.age)
})

What will be printed in the console?
medium
A. undefined
B. Alice
C. { name: 'Alice', age: 30 }
D. 30

Solution

  1. Step 1: Understand what is written to the file

    The object { name: 'Alice', age: 30 } is saved to 'data.json'.
  2. Step 2: Understand what is logged

    The code reads the file and logs content.age, which is 30.
  3. Final Answer:

    30 -> Option D
  4. Quick Check:

    Accessing content.age logs 30 [OK]
Hint: Read file content and access property to get value [OK]
Common Mistakes:
  • Expecting the whole object to print
  • Confusing name and age values
  • Assuming undefined because of async
4. You wrote this code to save JSON data:
cy.writeFile('user.json', '{"name": "Bob", "age": 25}')

But when you read the file, you get a string instead of an object. What is the problem?
medium
A. The JSON data is passed as a string, not as an object
B. The file path is incorrect
C. cy.writeFile cannot write JSON data
D. The file extension must be .txt

Solution

  1. Step 1: Analyze the data argument

    The data is passed as a JSON string, not as a JavaScript object.
  2. Step 2: Understand how cy.writeFile handles data

    If you pass a string, it writes it as a string. To write JSON, pass an object directly.
  3. Final Answer:

    The JSON data is passed as a string, not as an object -> Option A
  4. Quick Check:

    Pass object, not string, to write JSON [OK]
Hint: Pass object, not JSON string, to write JSON files [OK]
Common Mistakes:
  • Passing JSON as string instead of object
  • Blaming file path without checking data
  • Thinking file extension affects JSON writing
5. You want to append a new line 'Test passed' to an existing file 'results.txt' using cy.writeFile(). Which code snippet correctly appends the text without overwriting the file?
hard
A. cy.writeFile('results.txt', 'Test passed', { flag: 'a+' })
B. cy.writeFile('results.txt', 'Test passed')
C. cy.writeFile('results.txt', 'Test passed', { overwrite: false })
D. cy.writeFile('results.txt', 'Test passed', { append: true })

Solution

  1. Step 1: Understand file writing flags

    To append text, the file must be opened with append flag, which is 'a+' in Node.js.
  2. Step 2: Check Cypress options for cy.writeFile

    Cypress supports passing { flag: 'a+' } to append instead of overwrite.
  3. Final Answer:

    cy.writeFile('results.txt', 'Test passed', { flag: 'a+' }) -> Option A
  4. Quick Check:

    Use flag 'a+' to append text [OK]
Hint: Use { flag: 'a+' } option to append text [OK]
Common Mistakes:
  • Omitting flag option causes overwrite
  • Using non-existent options like append: true
  • Assuming overwrite: false appends text
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