---
title: Fixtures and Network Stubbing with cy.intercept | TestingBot
description: Because Cypress sits between your application and the network, it can
  watch every HTTP request the page makes, wait for it, check what was sent and, when
  you…
source_url:
  html: https://testingbot.com/resources/courses/cypress/fixtures-and-network-stubbing
  md: https://testingbot.com/resources/courses/cypress/fixtures-and-network-stubbing.md
---

Course

[Cypress Tutorial: First Spec to CI](https://testingbot.com/resources/courses/cypress)

Chapter 5 of 11

1. [1 Cypress Tutorial for Beginners](https://testingbot.com/resources/courses/cypress)
2. [2 Install Cypress and Run a First Spec](https://testingbot.com/resources/courses/cypress/install-and-first-spec)
3. [3 Selectors and the Cypress Command Queue](https://testingbot.com/resources/courses/cypress/selectors-and-the-command-queue)
4. [4 Cypress Assertions and Waiting Without Sleeps](https://testingbot.com/resources/courses/cypress/assertions-and-waiting)
5. [5 Fixtures and Network Stubbing with cy.intercept](https://testingbot.com/resources/courses/cypress/fixtures-and-network-stubbing)
6. [6 Custom Commands and Logging In Once with cy.session](https://testingbot.com/resources/courses/cypress/custom-commands-and-sessions)
7. [7 Cypress Configuration, Environment Variables and Retries](https://testingbot.com/resources/courses/cypress/config-and-environment-variables)
8. [8 Cypress Component Testing vs End-to-End Testing](https://testingbot.com/resources/courses/cypress/component-testing)
9. [9 Run Cypress on TestingBot and in CI](https://testingbot.com/resources/courses/cypress/run-cypress-on-testingbot)
10. [10 Debugging Cypress Tests](https://testingbot.com/resources/courses/cypress/debugging-cypress-tests)
11. [11 Knowledge check](https://testingbot.com/resources/courses/cypress/knowledge-check)

[Exit course](https://testingbot.com/resources/courses)

 Chapter 5 of 11 Beginner 6 min read 3 quick checks Updated Nov 2026 

# Fixtures and Network Stubbing with cy.intercept
[Watch this chapter Starts at 3:03 in the 9:03 course video.](https://www.youtube.com/watch?v=Ty406iWkgN4&t=183s)On this page
1. [Fixture files](https://testingbot.com#fixture-files)
2. [Spying: wait for a real request](https://testingbot.com#spying-wait-for-a-real-request)
3. [Stubbing: answer with your own data](https://testingbot.com#stubbing-answer-with-your-own-data)
4. [Checking what the app sent](https://testingbot.com#checking-what-the-app-sent)
5. [Changing part of a real response](https://testingbot.com#changing-part-of-a-real-response)
6. [cy.request: talk to the API without the page](https://testingbot.com#cy-request-talk-to-the-api-without-the-page)

Because Cypress sits between your application and the network, it can watch every HTTP request the page makes, wait for it, check what was sent and, when you want, answer it with data you control. This chapter covers `cy.intercept()`, fixture files and `cy.request()`, using the network demo page at `example.cypress.io/commands/network-requests`. Its "Get Comment" button fetches `https://jsonplaceholder.cypress.io/comments/1` and shows the comment body on the page.

## Fixture files[#](https://testingbot.com#fixture-files)

A fixture is a file of test data in `cypress/fixtures/`. Create `cypress/fixtures/comment.json`:

```json
{
  "id": 1,
  "name": "stubbed comment",
  "email": "learner@example.com",
  "body": "This came from cypress/fixtures/comment.json"
}
```

You can load it into a test with `cy.fixture()`:

```javascript
it('loads a fixture as test data', () => {
  cy.fixture('comment.json').then((comment) => {
    cy.visit('/todo')
    cy.get('[data-test=new-todo]').type(`${comment.name}{enter}`)
    cy.contains('.todo-list li', comment.name).should('exist')
  })
})
```

Fixtures are most useful as stubbed responses, which is next.

## Spying: wait for a real request[#](https://testingbot.com#spying-wait-for-a-real-request)

Create `cypress/e2e/network.cy.js`. The first test lets the request go to the real server but **spies** on it:

```javascript
describe('network requests', () => {
  beforeEach(() => {
    cy.visit('/commands/network-requests')
  })

  it('waits for the real request', () => {
    cy.intercept('GET', '**/comments/*').as('getComment')
    cy.get('.network-btn').click()

    cy.wait('@getComment').its('response.statusCode').should('eq', 200)
    cy.get('.network-comment').should('not.be.empty')
  })
})
```

- `cy.intercept(method, url)` registers a route. `**/comments/*` is a glob: any host, any comment id.
- `.as('getComment')` names it, and `cy.wait('@getComment')` waits until a matching request has completed, then yields an object with `request` and `response`.
- **Register the intercept before the action that triggers the request.** An intercept set up after the click never sees a request that has already gone out.

This is the replacement for `cy.wait(2000)` from chapter 3 when what you are really waiting for is the server.

A test clicks "Save" and then calls `cy.intercept('POST', '/api/items').as('save')` followed by `cy.wait('@save')`. It times out even though the request clearly happens. Why?

 The intercept was registered after the click, so it missed the request `cy.wait` needs a number of milliseconds as a second argument POST requests can only be stubbed, not spied on The alias must start with `@` in `.as()` too

Commands run in order, so the route only exists from the moment `cy.intercept` runs. Register it first, then do the action, then wait on the alias.

## Stubbing: answer with your own data[#](https://testingbot.com#stubbing-answer-with-your-own-data)

Pass a third argument and the request never reaches the server; Cypress answers it:

```javascript
it('stubs the response with a fixture', () => {
  cy.intercept('GET', '**/comments/*', { fixture: 'comment.json' }).as('getComment')
  cy.get('.network-btn').click()

  cy.wait('@getComment')
  cy.get('.network-comment').should('have.text', 'This came from cypress/fixtures/comment.json')
})
```

Stubbing gives you fast, deterministic tests for things that are hard to set up on a real backend: an empty list, a very long one, a 500 error, a slow response. The error path is a good example, because few teams ever test it otherwise:

```javascript
it('shows the error path when the server fails', () => {
  cy.intercept('PUT', '**/comments/*', {
    statusCode: 404,
    body: { error: 'comment not found' },
  }).as('putComment')
  cy.get('.network-put').click()

  cy.wait('@putComment')
  cy.get('.network-put-comment').should('contain', 'comment not found')
})
```

The page calls a different origin (`jsonplaceholder.cypress.io`), yet the stub needs no CORS headers: for a static response Cypress adds `access-control-allow-origin` (set to the page's origin) and `access-control-allow-credentials` itself, so the browser accepts it. That only applies to responses Cypress answers. A CORS error on a request that reaches the real server is a real bug, and the fix belongs on the server. Add `delay: 3000` to the same object to simulate a slow server, as chapter 3 did.

Your whole suite stubs every API call with fixtures. It is fast and green, but production broke when the backend renamed a JSON field. What does that tell you?

 The fixtures should have been loaded with `cy.fixture()` instead `cy.intercept` does not check response bodies Stubbed tests only test the front end against your assumptions; keep some tests that hit the real backend

A stub returns whatever you wrote, so it cannot notice the real API changing. Stub to test UI states that are hard to reach, and keep a smaller set of end-to-end tests that go through the real backend (spying with `cy.intercept` still lets you wait on those).

## Checking what the app sent[#](https://testingbot.com#checking-what-the-app-sent)

`cy.wait` yields the intercepted request, so you can assert on the payload as well as the response:

```javascript
it('checks what the app sent', () => {
  cy.intercept('POST', '**/comments').as('postComment')
  cy.get('.network-post').click()

  cy.wait('@postComment').then(({ request, response }) => {
    expect(request.body).to.include('email')
    expect(response.statusCode).to.eq(201)
  })
  cy.get('.network-post-comment').should('contain', 'POST successful!')
})
```

This page sends form-encoded data, so `request.body` is a string; for a JSON API it is an object and you can use `to.have.property('email')`.

## Changing part of a real response[#](https://testingbot.com#changing-part-of-a-real-response)

Between "let it through" and "answer it yourself" there is a third option: pass a function, let the request reach the server, and edit the response on its way back:

```javascript
it('changes part of a real response', () => {
  cy.intercept('GET', '**/comments/*', (req) => {
    req.continue((res) => {
      res.body.body = 'edited on the way back'
    })
  }).as('getComment')
  cy.visit('/commands/network-requests')
  cy.get('.network-btn').click()

  cy.wait('@getComment').its('response.body.id').should('eq', 1)
  cy.get('.network-comment').should('have.text', 'edited on the way back')
})
```

This keeps the real contract (status, headers, the other fields) while forcing the one value you care about, for example a feature flag or a date.

## cy.request: talk to the API without the page[#](https://testingbot.com#cy-request-talk-to-the-api-without-the-page)

`cy.intercept` deals with requests the **page** makes. `cy.request` makes a request from the test itself, outside the browser page, and is not subject to CORS:

```javascript
it('calls an API directly with cy.request', () => {
  cy.request('https://jsonplaceholder.cypress.io/comments/1').then((res) => {
    expect(res.status).to.eq(200)
    expect(res.body).to.have.property('email')
  })
})
```

Use it to seed data before a test, clean up after one, or log in without the UI. That last use is the subject of the next chapter.

Before each test you need three todo items to exist on the server. Which approach is the most reliable?

 Type them into the form in `beforeEach` and wait 1 second after each Create them with `cy.request()` calls to the API in `beforeEach` Create them in the first test and let the other tests reuse them

Setting up data through the API is fast and does not depend on the UI you are about to test. Typing through the form is slow and tests the form again in every test; relying on an earlier test breaks as soon as tests run alone or in a different order.

[Previous · Chapter 4 Cypress Assertions and Waiting Without Sleeps](https://testingbot.com/resources/courses/cypress/assertions-and-waiting) [Next · Chapter 6 Custom Commands and Logging In Once with cy.session](https://testingbot.com/resources/courses/cypress/custom-commands-and-sessions)

On this page

1. [Fixture files](https://testingbot.com#fixture-files)
2. [Spying: wait for a real request](https://testingbot.com#spying-wait-for-a-real-request)
3. [Stubbing: answer with your own data](https://testingbot.com#stubbing-answer-with-your-own-data)
4. [Checking what the app sent](https://testingbot.com#checking-what-the-app-sent)
5. [Changing part of a real response](https://testingbot.com#changing-part-of-a-real-response)
6. [cy.request: talk to the API without the page](https://testingbot.com#cy-request-talk-to-the-api-without-the-page)

Certification

Earn the free, resume-worthy TestingBot Cypress Certification for this course.

[How it works](https://testingbot.com/resources/courses/cypress/certification)
