---
id: api/commands/fixture
title: cy.fixture()
description: Load a fixed set of data located in a file in Cypress.
section: api
source_path: docs/api/commands/fixture.mdx
version: 3163d68b20e695f2c76d40c85c3f3b956dd19a3b
updated_at: '2026-08-21T20:59:04.402Z'
---
# fixture

Load a fixed set of data located in a file.

## Syntax

```
cy.fixture(filePath)
cy.fixture(filePath, encoding)
cy.fixture(filePath, options)
cy.fixture(filePath, encoding, options)
```

### Usage

**Correct Usage**

```
cy.fixture('users').as('usersJson') // load data from users.json
cy.fixture('logo.png').then((logo) => {
  // load data from logo.png
})
```

### Arguments

**filePath _(String)_**

A path to a file within the [`fixturesFolder`](/llm/markdown/app/references/configuration.md#Folders--Files) , which defaults to `cypress/fixtures`.

You can nest fixtures within folders and reference them by defining the path from the fixturesFolder:

```
cy.fixture('users/admin.json') // Get data from {fixturesFolder}/users/admin.json
```

**encoding _(String)_**

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

*   `'ascii'`
*   `'base64'`
*   `'binary'`
*   `'hex'`
*   `'latin1'`
*   `'utf8'`
*   `'utf-8'`
*   `'ucs2'`
*   `'ucs-2'`
*   `'utf16le'`
*   `'utf-16le'`
*   `null`

Using `null` explicitly will return the fixture as a [`Cypress.Buffer`](/llm/markdown/api/utilities/buffer.md) instance, regardless of file extension.

**options _(Object)_**

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

| Option | Default | Description |
| --- | --- | --- |
| `timeout` | [`responseTimeout`](/llm/markdown/app/references/configuration.md#Timeouts) | Time to wait for `cy.fixture()` to resolve before [timing out](#Timeouts) |

### Yields

*   `cy.fixture()` yields the contents of the file. Formatting is determined by its file extension.
*   The yielded subject is **not** updated if the contents change on disk.

## Examples

### JSON

#### Load a `users.json` fixture

```
cy.fixture('users.json').as('usersData')
```

#### Omit the fixture file's extension

When no extension is passed to `cy.fixture()`, Cypress will search for files with the specified name within the [`fixturesFolder`](/llm/markdown/app/references/configuration.md#Folders--Files) (which defaults to `cypress/fixtures`) and resolve the first one.

```
cy.fixture('admin').as('adminJSON')
```

The example above would resolve in the following order:

1.  `cypress/fixtures/admin.json`
2.  `cypress/fixtures/admin.js`
3.  `cypress/fixtures/admin.coffee`
4.  `cypress/fixtures/admin.html`
5.  `cypress/fixtures/admin.txt`
6.  `cypress/fixtures/admin.csv`
7.  `cypress/fixtures/admin.png`
8.  `cypress/fixtures/admin.jpg`
9.  `cypress/fixtures/admin.jpeg`
10.  `cypress/fixtures/admin.gif`
11.  `cypress/fixtures/admin.tif`
12.  `cypress/fixtures/admin.tiff`
13.  `cypress/fixtures/admin.zip`

#### Use import statement

If you are loading a JSON fixture, you can simply use the `import` statement and let the bundler load it:

```
// cypress/e2e/spec.cy.js
import user from '../fixtures/user.json'
it('loads the same object', () => {
  cy.fixture('user').then((userFixture) => {
    expect(user, 'the same data').to.deep.equal(userFixture)
  })
})
```

### Images

#### Image fixtures are sent as `base64` by default

```
cy.fixture('images/logo.png').then((logo) => {
  // logo will be encoded as base64
  // and should look something like this:
  // aIJKnwxydrB10NVWqhlmmC+ZiWs7otHotSAAAOw==...
})
```

#### Change encoding of Image fixture

```
cy.fixture('images/logo.png', null).then((logo) => {
  // logo will be read as a buffer
  // and should look something like this:
  // Buffer([0, 0, ...])
  expect(Cypress.Buffer.isBuffer(logo)).to.be.true
})
```

### Playing MP3 file

```
cy.fixture('audio/sound.mp3', 'base64').then((mp3) => {
  const uri = 'data:audio/mp3;base64,' + mp3
  const audio = new Audio(uri)

  audio.play()
})
```

### Accessing Fixture Data

#### Using `.then()` to access fixture data

```
cy.fixture('users').then((json) => {
  cy.intercept('GET', '/users/**', json)
})
```

#### Using fixtures to bootstrap data

[Check out our example recipe using `cy.fixture()` to bootstrap data for our application.](/llm/markdown/app/references/recipes.md#Server-Communication)

#### Modifying fixture data before using it

You can modify fixture data directly before visiting a URL or mounting a component that makes a network request to that URL.

*   End-to-End Test
*   Component Test

```
cy.fixture('user').then((user) => {
  user.firstName = 'Jane'
  cy.intercept('GET', '/users/1', user).as('getUser')
})

cy.visit('/users')
cy.wait('@getUser').then(({ request }) => {
  expect(request.body.firstName).to.eq('Jane')
})
```

```
cy.fixture('user').then((user) => {
  user.firstName = 'Jane'
  cy.intercept('GET', '/users/1', user).as('getUser')
})

cy.mount(<Users />)
cy.wait('@getUser').then(({ request }) => {
  expect(request.body.firstName).to.eq('Jane')
})
```

## Notes

### Shortcuts

#### Using the `fixture` `StaticResponse` property

Fixtures can also be referenced directly without using the `.fixture()` command by using the special property `fixture` on the [`cy.intercept()`](/llm/markdown/api/commands/intercept.md) `StaticResponse` object.

```
cy.intercept('GET', '/users/**', { fixture: 'users' })
```

### Validation

#### Automated File Validation

Cypress automatically validates your fixtures. If your `.json`, `.js`, or `.coffee` files contain syntax errors, they will be shown in the Command Log.

### Encoding

#### Default Encoding

Cypress automatically determines the encoding for the following file types:

*   `.json`
*   `.js`
*   `.coffee`
*   `.html`
*   `.txt`
*   `.csv`
*   `.png`
*   `.jpg`
*   `.jpeg`
*   `.gif`
*   `.tif`
*   `.tiff`
*   `.zip`

For other types of files, they will be read as `utf8` by default, unless specified in the second argument of `cy.fixture()`. You can specify `null` as the encoding in order to read the file as a [`Cypress.Buffer`](/llm/markdown/api/utilities/buffer.md) instance instead.

### `this` context

If you store and access the fixture data using `this` test context object, make sure to use `function () { ... }` callbacks. Otherwise the test engine will NOT have `this` pointing at the test context.

```
describe('User page', () => {
  beforeEach(function () {
    // "this" points at the test context object
    cy.fixture('user').then((user) => {
      // "this" is still the test context object
      this.user = user
    })
  })

  // the test callback is in "function () { ... }" form
  it('has user', function () {
    // this.user exists
    expect(this.user.firstName).to.equal('Jane')
  })
})
```

### Large Files

#### Memory and Transfer Constraints

`cy.fixture()` reads the **entire file into memory** and transfers its contents from the Cypress Node.js server process to the browser test runner over an internal WebSocket connection. There is no explicit file size limit in Cypress itself, but large files — especially binary files like `.zip` archives — can cause Cypress to crash or become unresponsive due to:

*   **Memory pressure**: The complete file must fit in available RAM and is held in the fixture cache for the duration of the test run.
*   **WebSocket message size**: File contents are sent as a single message over the internal socket. Payloads that approach or exceed 100 MB can exceed socket-level buffer limits.

As a practical guideline, avoid using `cy.fixture()` with files larger than a few megabytes. Smaller test fixtures (representative subsets of production data) are preferable wherever possible.

#### Uploading Large Files via a File Input

If your goal is to simulate a file upload through an `<input type="file">` element, use [`.selectFile()`](/llm/markdown/api/commands/selectfile.md) with a path string rather than loading the fixture first and then setting the input manually:

```
// ✅ Preferred for file upload simulation
cy.get('input[type="file"]').selectFile('cypress/fixtures/large.zip')
```

#### Keeping Large File Operations Server-Side

For scenarios where you must work with large files but do not need to transfer their full contents to the browser (for example, verifying file metadata or invoking a server-side operation), use [`cy.task()`](/llm/markdown/api/commands/task.md) to keep the operation in the Node.js context:

```
// cypress.config.js
const { defineConfig } = require('cypress')
const fs = require('fs')

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      on('task', {
        getFileSize(filePath) {
          const { size } = fs.statSync(filePath)
          return size
        },
      })
    },
  },
})
```

```
// in your test
cy.task('getFileSize', 'cypress/fixtures/large.zip').then((size) => {
  expect(size).to.be.greaterThan(0)
})
```

### Loaded just once

Please keep in mind that fixture files are assumed to be unchanged during the test, and thus Cypress loads them just once. Even if you overwrite the fixture file itself, the already loaded fixture data remains the same.

If you wish to dynamically change the contents of a file during your tests, consider [`cy.readFile()`](/llm/markdown/api/commands/readfile.md) instead.

For example, if you want to reply to a network request with different object, the following **will not work**:

```
// 🚨 DOES NOT WORK
cy.intercept('GET', '/todos/1', { fixture: 'todo' }).as('todo')
// application requests the /todos/1 resource
// the intercept replies with the object from todo.json file

cy.wait('@todo').then(() => {
  cy.writeFile('/cypress/fixtures/todo.json', { title: 'New data' })
})
// application requests the /todos/1 resource again
// the intercept replies with the originally loaded object
// from the todo.json file and NOT { "title": "New data" }
```

In this situation, avoid using the fixture file and instead respond to the network request with the object

```
// ✅ RESPOND WITH OBJECT
cy.fixture('todo.json').then((todo) => {
  cy.intercept('GET', '/todos/1', { body: todo }).as('todo')
  // application requests the /todos/1 resource
  // the intercept replies with the initial object

  cy.wait('@todo').then(() => {
    // modify the response object
    todo.title = 'New data'
    // and override the intercept
    cy.intercept('GET', '/todos/1', { body: todo })
  })
})
```

## Rules

### Requirements

*   `cy.fixture()` requires being chained off of `cy`.

### Assertions

*   `cy.fixture()` will only run assertions you have chained once, and will not [retry](/llm/markdown/app/core-concepts/retry-ability.md).

### Timeouts

*   `cy.fixture()` should never time out.

Because `cy.fixture()` 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

*   `cy.fixture()` does _not_ log in the Command Log

## See also

*   [Guide: Variables and Aliases](/llm/markdown/app/core-concepts/variables-and-aliases.md)
*   [`cy.intercept()`](/llm/markdown/api/commands/intercept.md)
*   [`.then()`](/llm/markdown/api/commands/then.md)
*   [`.readFile()`](/llm/markdown/api/commands/readfile.md) for a similar command without caching and with builtin retryability
*   [`.selectFile()`](/llm/markdown/api/commands/selectfile.md) for simulating file uploads, including with large files
*   [`cy.task()`](/llm/markdown/api/commands/task.md) for keeping large file operations in the Node.js context
*   [Recipe: Bootstrapping App Test Data](/llm/markdown/app/references/recipes.md#Server-Communication)
