Improve this doc

Write to a file with the specified contents.


cy.writeFile(filePath, contents)
cy.writeFile(filePath, contents, encoding)
cy.writeFile(filePath, contents, options)


Correct Usage



filePath (String)

A path to a file within the project root (the directory that contains cypress.json).

contents (String, Array, or Object)

The contents to be written to the file.

encoding (String)

The encoding to be used when writing to the file. The following encodings are supported:

  • ascii
  • base64
  • binary
  • hex
  • latin1
  • utf8
  • utf-8
  • ucs2
  • ucs-2
  • utf16le
  • utf-16le

options (Object)

Pass in an options object to change the default behavior of cy.writeFile().

Option Default Description
log true Displays the command in the Command log
flag w File system flag as used with fs.writeFile
encoding utf8 The encoding to be used when writing to the file

To use encoding with other options, have your options object be your third parameter and include encoding there. This is the same behavior as fs.writeFile.


  • cy.writeFile() yields null.

  • cy.writeFile() cannot be chained further.



Write some text to a txt file

If the path to the file does not exist, the file and its path will be created. If the file already exists, it will be over-written.

  .writeFile('path/to/message.txt', 'Hello World')
  .then((text) => {
    expect(text).to.equal('Hello World') // true

{projectRoot}/path/to/message.txt will be created with the following contents:

 "Hello World"


Write JSON to a file

JavaScript arrays and objects are stringified and formatted into text.

cy.writeFile('path/to/data.json', { name: 'Eliza', email: '[email protected]' })
  .then((user) => {

{projectRoot}/path/to/data.json will be created with the following contents:

  "name": "Eliza",
  "email": "[email protected]"

Write response data to a fixture file

cy.request('https://jsonplaceholder.typicode.com/users').then((response) => {
  cy.writeFile('cypress/fixtures/users.json', response.body)

// our fixture file is now generated and can be used
cy.fixture('users').then((users) => {


Specify the encoding as a String

cy.writeFile('path/to/ascii.txt', 'Hello World', 'ascii'))

{projectRoot}/path/to/message.txt will be created with the following contents:

Hello World

Specify the encoding as part of the options object

cy.writeFile('path/to/ascii.txt', 'Hello World', { encoding: 'ascii', flag: 'a+' })


Append contents to the end of a file

cy.writeFile('path/to/message.txt', 'Hello World', { flag: 'a+' })



  • cy.writeFile() requires being chained off of cy.

  • cy.writeFile() requires the file be successfully written to disk. Anything preventing this such as OS permission issues will cause it to fail.


  • cy.writeFile() will only run assertions you've chained once, and will not retry.


  • cy.writeFile() should never time out.

Because cy.writeFile() is asynchronous it is technically possible for there to be a timeout while talking to the internal Cypress automation APIs. But for practical purposes it should never happen.

Command Log

Write an array to a file

cy.writeFile('info.log', ['foo', 'bar', 'baz'])

The command above will display in the Command Log as:

Command Log

When clicking on the writeFile command within the command log, the console outputs the following:

Console Log


Version Changes
3.1.1 Added flag option and appending with a+
1.0.0 Introduced

See also