0
0
Cypresstesting~3 mins

Why Stubbing responses in Cypress? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your tests never had to wait for a slow or broken server again?

The Scenario

Imagine testing a web app that fetches data from a server. You have to wait for the server to respond every time you test. Sometimes the server is slow or down, so you can't test properly.

The Problem

Manually waiting for real server responses is slow and frustrating. Tests can fail just because the server is unavailable, not because the app is broken. This makes testing unreliable and wastes time.

The Solution

Stubbing responses lets you fake the server replies. You tell your test what data to expect without calling the real server. This makes tests fast, stable, and predictable.

Before vs After
Before
cy.visit('/page');
cy.wait(5000); // waiting for real server response
After
cy.intercept('GET', '/api/data', { fixture: 'data.json' });
cy.visit('/page');
What It Enables

Stubbing responses enables fast, reliable tests that work even when the real server is slow or offline.

Real Life Example

When testing a shopping site, you can stub the product list response to always show the same items. This way, your tests won't break if the real product database changes or is down.

Key Takeaways

Manual testing with real servers is slow and unreliable.

Stubbing responses fakes server data for fast, stable tests.

This helps catch real app bugs without external delays.