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.
- Stub
- Load
- Reuse
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| The rain machine | A network stub |
| The prop box | A fixture file |
| The stage directions | A custom command |
| Filming without a storm | Testing 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
- 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)
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.
- 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)
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.
- 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)
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.