Skip to article
detaID ↗
← Back home

Simulating Time in Cypress with cy.clock and cy.tick

When writing Cypress tests, there are scenarios where you need to manipulate time. For example, testing session expiration or scheduled UI updates requires control over time progression. This is where `cy.clock()` and `cy.tick()` come in handy.

1 min read
#Testing#Cypress#E2E Testing

When writing Cypress tests, there are scenarios where you need to manipulate time. For example, testing session expiration or scheduled UI updates requires control over time progression. This is where cy.clock() and cy.tick() come in handy.

Controlling Time with cy.clock()

cy.clock() overrides the browser's native time functions like Date, setTimeout, setInterval, and clearTimeout. This allows you to freeze time at a specific point and manipulate it as needed.

Example:

js
cy.clock(new Date()); // Freezes the time at the current moment

Fast-forwarding Time with cy.tick()

Once the clock is controlled, you can use cy.tick() to move time forward artificially. This is useful for testing delays, timeouts, or token expiration scenarios.

Example:

js
describe("User session expiration", () => { it("logs out user after token expiration", () => { cy.visit("/auth/sign-in"); // Simulate user login cy.get('[data-testid="loginButton"]').click(); cy.getCookie("authToken").should("exist"); cy.clock(); // Fast-forward 30 minutes cy.tick(1000 * 60 * 30); // Attempt to navigate and verify user is logged out cy.visit("/dashboard"); cy.url().should("include", "/auth/sign-in"); }); });

When to Use cy.clock() and cy.tick()

  • Testing session expiration
  • Simulating time-based UI changes (e.g., countdowns, auto-refresh)
  • Validating delays without slowing down tests

Using cy.clock() and cy.tick() helps create reliable and fast tests by eliminating unpredictable delays and ensuring time-based logic is properly verified.

More writing

A few more notes from my archive.

Maestro E2E Testing on a React Native Debug Build

Most Maestro guides assume you're testing against a release build. But during active development, you naturally want to test against the debug build that's already running. Here's how to handle the Metro bundler dev menu overlay that can break your tests.

Testing Priorities in the AI Era: Smoke Tests and Documentation First

My take on modern testing priorities: start with smoke tests and documentation, then layer in e2e, component, and unit tests. A practical approach for AI-powered development.

Why Manual Test Cases Still Matter: When Code Can't Speak, Scenarios Must Do the Talking

Why manual test cases still matter even when automated testing is the norm — with practical guidance for development teams.

The Difference Between Functional Testing and Visual Testing

Functional testing checks if an app works as expected, while visual testing ensures the UI looks right. Combining both keeps the app functional and visually consistent.

Leave a comment