cypress-documentation icon indicating copy to clipboard operation
cypress-documentation copied to clipboard

cypress restore clock is not working

Open sameerthekhans opened this issue 1 year ago • 0 comments

Description

Cypress clock fast forward the time but not restores it to original within same tests

URL of Issue(s)

https://docs.cypress.io/api/commands/clock#Examples

Steps to replicate

I am able to reproduce the issue in the given example from the official docs

Step 1: Create index.html and serve it with any server(I am using vscode extension live server)

<div id="seconds-elapsed"></div>
    <script>
        window.addEventListener('DOMContentLoaded', () => {
            let seconds = 0;
      
            setInterval(() => {
              document.querySelector("#seconds-elapsed").textContent = ++seconds + " seconds";
            }, 1000);
        })
    </script>

Step 2: Write the cypress test

it.only('Test', () => {
  cy.clock(+ new Date())
  cy.visit('http://127.0.0.1:5500/')
  cy.tick(5000)
  cy.get('#seconds-elapsed').should('have.text', '5 seconds')
  cy.tick(1000);
  cy.get('#seconds-elapsed').should('have.text', '6 seconds')
  //Restore way 1
  cy.tick(0).then(clock => {
    clock.restore();
  })
  //Restore way 2
  // cy.clock().invoke('restore')
  //Restore way 3
  // cy.tick(0).invoke('restore');
  //Restore way 4
  // cy.clock().then(clock => clock.restore());
});

When the test is run I can see it fast-forwarded to 5sec and then 6sec, but after restore it is freeze at 6sec

My expectation is it should resume the original interval timer and update the text to 7, 8sec...

I am using Cypress - 12.8.1

Browser

Google Chrome 112.0.5615.49

Device

  • [ ] PC
  • [X] Mac
  • [ ] iPhone
  • [ ] iPad
  • [ ] Android Phone
  • [ ] Android Tablet

Additional Information

I am actually using cypress for angular project and as the restore was not working I tried for the given example from official docs

sameerthekhans avatar Apr 06 '23 01:04 sameerthekhans