Open In App

Cypress - tick() Method

Last Updated : 10 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The tick() method in Cypress is a powerful utility that allows you to simulate the passage of time in your tests. It is particularly useful when dealing with functions like setTimeout, setInterval, or any code that requires a delay or timed events. By manually advancing the clock, you can skip wait times, making your tests run faster and more predictably.

Usages of Cypress - tick() Method

The tick() method is often used when testing:

  • Animations or UI components with delays.
  • Functions that rely on timers (e.g., setTimeout and setInterval).
  • Delayed AJAX calls or other asynchronous operations.

In Cypress, you typically combine cy.clock() with cy.tick() to control the JavaScript clock in the browser. cy.clock() freezes the time, while tick() advances it by a specified amount.

Syntax

cy.tick(milliseconds)

Arguments

  • milliseconds: (Number) The number of milliseconds by which to advance the clock.

Example 1: Using tick() with setTimeout

HTML Code

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>SetTimeout Example</title>
</head>
<body>
  <button id="start">Start Timer</button>
  <div id="message"></div>

  <script>
    document.getElementById('start').addEventListener('click', function() {
      setTimeout(function() {
        document.getElementById('message').innerText = 'Timer finished!';
      }, 5000);
    });
  </script>
</body>
</html>

Cypress Test Code

JavaScript
describe('setTimeout Example with tick()', () => {
  it('should display message after 5 seconds using tick()', () => {
    // Visit the page
    cy.visit('http://localhost:3000');

    // Freeze the clock before the interaction
    cy.clock();

    // Click the 'Start Timer' button
    cy.get('#start').click();

    // Fast-forward time by 5 seconds (5000 milliseconds)
    cy.tick(5000);

    // Check if the message is displayed after 5 seconds
    cy.get('#message').should('have.text', 'Timer finished!');
  });
});

Explanation

In this example, the button starts a timer that updates a message after 5 seconds. The Cypress test uses cy.clock() to freeze time and cy.tick(5000) to fast-forward by 5 seconds. This allows us to test the functionality without actually waiting.

Output:

output
Output

Example 2: Using tick() with setInterval

HTML Code

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>SetInterval Example</title>
</head>
<body>
  <div id="counter">0</div>
  
  <script>
    let count = 0;
    setInterval(function() {
      count++;
      document.getElementById('counter').innerText = count;
    }, 1000);
  </script>
</body>
</html>

Cypress Test Code

JavaScript
describe('setInterval Example with tick()', () => {
  it('should update the counter every second using tick()', () => {
    cy.visit('http://localhost:3000');

    // Freeze the clock
    cy.clock();

    // Fast-forward time by 3 seconds (3000 milliseconds)
    cy.tick(3000);

    // Check if the counter displays 3
    cy.get('#counter').should('have.text', '3');
  });
});

Explanation

This example increments a counter every second using setInterval. The Cypress test uses cy.clock() to freeze time and cy.tick(3000) to advance time by 3 seconds. It checks if the counter value has correctly updated to "3" after fast-forwarding time.

Output:

output
Output

Conclusion

The tick() method is a valuable tool for simulating the passage of time in tests, enabling you to efficiently test time-based behaviors without waiting for real-time delays. Combining it with cy.clock(), you can gain full control over timers and animations in your application, making your tests run faster and more reliably.


Next Article
Article Tags :

Similar Reads