0
0
Cypresstesting~3 mins

Why Dynamic response stubbing in Cypress? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could control server answers instantly to test anything you want without waiting?

The Scenario

Imagine testing a web app that fetches user data from a server. You manually change the server data each time to test different user profiles. This means waiting, refreshing, and hoping the server responds as expected.

The Problem

Manually changing server data is slow and tiring. It's easy to make mistakes or forget to reset data. Tests become flaky because the server might be slow or down. You waste time waiting instead of testing.

The Solution

Dynamic response stubbing lets you fake server responses right inside your test. You control what data the app gets instantly, without changing the real server. This makes tests fast, reliable, and easy to repeat.

Before vs After
Before
Visit app -> Wait for server -> Change server data manually -> Refresh -> Check UI
After
cy.intercept('/api/user', { fixture: 'user1.json' }).as('getUser');
cy.visit('/');
cy.wait('@getUser');
// UI shows user1 data
What It Enables

Dynamic response stubbing makes testing flexible and fast by letting you control server answers on the fly.

Real Life Example

Testing a shopping cart app where you stub different product lists to check how the cart updates without needing real products on the server.

Key Takeaways

Manual server changes slow down and complicate testing.

Dynamic stubbing fakes server responses instantly inside tests.

This leads to faster, more reliable, and repeatable tests.