Search⌘ K
AI Features

Spies, Stubs, and Clocks

Explore how to use Cypress features like spies stubs and clocks to control application behavior during testing. Learn to modify function responses spy on calls and manage time to create reliable and efficient end-to-end tests.

First and foremost, why do we need to control some of the behaviors of our application? Wouldn’t we want to test it from a user’s perspective without modifying anything programmatically? There can be several reasons for controlling the original behavior of our app. Let’s take a look at some.


Reasons for controlling behavior

When we want to modify the behavior of our application, here are a couple of scenarios that will make our test suite more robust:

  • Avoid side effects, such as a bad network connection
  • We are dealing with Promises, and we want to automatically resolve or reject them.
  • We want to force things to fail to test a failure path.
  • We want to force things to pass to test a happy path.
  • We want to speed up the test suite by shortcutting redundant steps like logging in to test
...