TOPIC 33/4016 MINNOT STARTED

Cypress: stub the network, load fixtures, write custom commands

Control what the server sends back, keep test data in files, and wrap repeated steps into your own commands. This is the second step after your first Cypress test.

L1–L4 are part of the Pro pass

L0 Explain simply

Analogy: a film set. A film crew does not need a real storm to film one. They use a rain machine, a prop box and a card of stage directions.

The scene is the same each time.

A stub is the rain machine. Tom, the tester at Daily List, a to-do list app, uses it to control what the server says. The aim is to test the screen on its own terms, with the server's answer fixed by you.

PLAIN WORDS · 1 in this section
stub
a simple fake part that gives fixed answers

PICTURE IT

The picture is the one the lesson above opens with. The table below shows how each part of it matches the software.

THE REAL JOB A film crew using a rain machine, a prop box and a card of stage directions.

  1. Stub
  2. Load
  3. Reuse
Film set

HOW THE PICTURE MAPS

How each part of the picture matches the software
In the pictureIn the software
The rain machineA network stub
The prop boxA fixture file
The stage directionsA custom command
Filming without a stormTesting without a real server

WHERE THIS PICTURE STOPS

  • A rain machine looks fake on screen, but a stub is hidden from the app.
  • A film has one take, but a test runs many times.
  • A crew buys props, but fixtures are free text files.

HOOK

Fake the weather, keep the props, write the directions.

TELL A FRIEND

A film set: fake the storm, keep props in a box, write the directions once.

OFTEN MIXED UP WITH Cypress: your first end-to-end test. That topic writes the first Cypress test. This one controls the server reply and removes repeated steps.

PRACTISE AND KEEP

Sources for this topic — 3 sources
  1. Market practice

    cy.intercept() (Cypress docs) ↗ — Cypress · Cypress docs: the intercept command can spy on, stub and change network requests, and a route can return a fixed response from a fixture file (search excerpt, page not opened)

    The reference page for spying on, stubbing and changing network requests in a Cypress test. Official project documentation. · Checked 2026-10-31

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.

  2. Market practice

    Intercepting network requests (Cypress docs) ↗ — Cypress · Cypress docs: a stub lets you control the body, status, headers and delay of a response, and an intercept should be set before the page visit so it does not miss early requests (search excerpt, page not opened)

    A guide to stubbing responses, waiting on requests and controlling the body, status, headers and delay. Official project documentation. · Checked 2026-10-31

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.

  3. Market practice

    Custom commands (Cypress docs) ↗ — Cypress · Cypress docs: a custom command is added once, usually in the support file that runs before every spec, and plain functions are often better when only one spec needs the step (search excerpt, page not opened)

    The reference for adding and overwriting commands, and where to keep them. Official project documentation. · Checked 2026-10-31

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.

Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.