Reputation: 971
I am trying to set the time of the browser so I can do some time-based validation. Unfortunately when I set the time as below intervals do not trigger. I can manually make the intervals fire by ticking. Is there a way to allow the time ticking to continue after setting the date?
const dateToSet = new Date(date).getTime()
cy.clock(dateToSet, ["Date"])
Upvotes: 8
Views: 10166
Reputation: 921
Just overwrite Date
alone:
cy.clock(Date.UTC(2020, 6, 23), ['Date']);
Upvotes: 18
Reputation: 7777
Looking at the cypress documentation, you can then use cy.tick()
to simulate time.
I think that cypress assumes you want to control time yourself when you invoke the clock, sounds like reasonable behaviour to me.
cy.clock() yields a clock object with the following methods:
clock.tick(milliseconds)
Move the clock the specified number of milliseconds. Any timers within the affected range of time will be called.
clock.restore()
Restore all overridden native functions. This is automatically called between tests, so should not generally be needed.
You can also access the clock object via this.clock in a .then() callback.
Read more at https://docs.cypress.io/api/commands/clock.html#Syntax
Upvotes: -1