To load a fixture, use the cy.fixture() command.

// Instead of writing a response inline you can
// use a fixture file's content.

// when application makes an Ajax request matching "GET **/comments/*"
// Cypress will intercept it and reply with the object in `example.json` fixture
cy.intercept('GET', '**/comments/*', { fixture: 'example.json' }).as('getComment')

// we have code that gets a comment when
// the button is clicked in scripts.js

  .should('', 'name')
  .and('include', 'Using fixtures to represent data')

cy.fixture or require

You can use require to load JSON fixtures.

/// JSON fixture file can be loaded directly using
// the built-in JavaScript bundler
const requiredExample = require('../../fixtures/example')

beforeEach(() => {
  // load example.json fixture file and store
  // in the test context object

it('cy.fixture or require', function () {
  // we are inside the "function () { ... }"
  // callback and can use test context object "this"
  // "this.example" was loaded in "beforeEach" function callback
  expect(this.example, 'fixture in the test context')

  // or use "cy.wrap" and "should('deep.equal', ...)" assertion
  cy.wrap(this.example, 'fixture vs require')
    .should('deep.equal', requiredExample)


To read a file's content, use the cy.readFile() command.

// You can read a file and yield its contents
// The filePath is relative to your project's root.
cy.readFile('cypress.config.js').then((json) => {


To write to a file with the specified contents, use the cy.writeFile() command.

// You can write to a file

// Use a response from a request to automatically
// generate a fixture file for use later
  .then((response) => {
    cy.writeFile('cypress/fixtures/users.json', response.body)
cy.fixture('users').should((users) => {

// JavaScript arrays and objects are stringified
// and formatted into text.
cy.writeFile('cypress/fixtures/profile.json', {
  id: 8739,
  name: 'Jane',
  email: '[email protected]',

cy.fixture('profile').should((profile) => {