How to set current time to future time by using Cypress?

1.7k Views Asked by At

I am using cypress to automate countdown content block, I need to test recurrence scenarios by using Cypress. In Example,

At the Start - Days - 00 | Hrs - 00 | Mins - 00

I need to validate, after one hour, count down will be started (Days - 23 | Hrs - 59 | Mins - 59)

I cannot use hard-coded wait times, so I try the following cypress script but It won't work.

const now = new Date().getTime()
cy.clock(now)
cy.tick(3600000)

Anything worng in this? Or help me to find a good solution.

2

There are 2 best solutions below

0
On BEST ANSWER

You can just create the currentNow you want and then set it

    const now = new Date(2011, 0, 11) // month is 0-indexed
    cy.clock(now)
1
On

I would recommend to use moment library, so it will take current time + one hour, and so on

const afterOneHour = moment().add(+1, 'h').format('HH:mm');

like on example here