Skip to content

bahmutov/cypress-rest-easy

Repository files navigation

cypress-rest-easy

Quickly creates REST mocks for your Cypress tests

Install

$ npm i -D cypress-rest-easy

Add this plugin to your support / spec file

// https://github.com/bahmutov/cypress-rest-easy
import 'cypress-rest-easy'

Use

rest

Declare the REST endpoints in the describe / it configuration block

// "todos.json" is a Cypress fixture file
describe('Todos', { rest: { todos: 'todos.json' } }, () => {
  // each test will have mock backend with the following endpoints
  // GET /todos
  // GEt /todos/:id
  // POST /todos
  // DELETE /todos/:id
  // PATCH /todos/:id

The above syntax creates automatic intercepts with aliases:

Automatic mocks

The fixture file should be an array of items.

See todos.cy.js and todos.json for examples

The mocks and data are automatically reset before each test.

You can use simply array for the resource

{
  rest: {
    assignId: true,
    todos: [
      {
        id: '101-102-103',
        title: 'First item',
        completed: true,
      },
    ],
  },
}

See the spec file list-as-resource.cy.js

data access

You can get the "live" data for each resource by name, for example

it('adds a todo', { rest: { todos: 'todos.json' } }, () => {
  // use the REST resource name
  const todos = Cypress.env('todos')
  const n = todos.length
  cy.visit('/')
  cy.get('li.todo').should('have.length', n)
  cy.get('input.new-todo')
    .type('Write tests{enter}')
    // there should be one more item in the array
    .then(() => {
      expect(todos).to.have.length(n + 1)
    })
})

You need cy.then to access the changed data after Cypress commands have finished. Alternatively, you can wrap the array reference:

cy.get('input.new-todo').type('Write tests{enter}')
// there should be one more item in the array
cy.wrap(todos).should('have.length', n + 1)

baseUrl

If all your REST endpoints use the same prefix, you can set the baseUrl option

describe(
  'Todos with base URL',
  { rest: { baseUrl: '/api/v1', todos: 'todos.json' } },
  () => {

Base URL option routes

assignId

Typically, the backend creates new id for each POST /resource call that does not send the id property. You can create UUID values automatically:

{ rest: { assignId: true, todos: 'todos.json' } }

You can also pass your own synchronous function to return an id

{
  rest: {
    assignId: () => '123-abc',
    todos: 'todos.json',
  },
}

See also

Small print

Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2025

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

About

Quickly creates REST mocks for your Cypress tests

Topics

Resources

License

Stars

Watchers

Forks