Quickly creates REST mocks for your Cypress tests
- 📝 read the blog post Rest Easy
- 📝 read the blog post Rest Easy Example
- 📺 watch the video cypress-rest-easy Plugin Introduction
$ npm i -D cypress-rest-easyAdd this plugin to your support / spec file
// https://github.com/bahmutov/cypress-rest-easy
import 'cypress-rest-easy'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/:idThe above syntax creates automatic intercepts with aliases:
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
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)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' } },
() => {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',
},
}- plugin cypress-magic-backend
Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2025
- @bahmutov
- glebbahmutov.com
- blog
- videos
- presentations
- cypress.tips
- Cypress Tips & Tricks Newsletter
- my Cypress courses
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

