最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cypress how to set the date without affecting intervals - Stack Overflow

programmeradmin1浏览0评论

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"])

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"])

Share Improve this question asked Jan 22, 2020 at 12:20 Westy10101Westy10101 9713 gold badges14 silver badges27 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 18

Just overwrite Date alone:

cy.clock(Date.UTC(2020, 6, 23), ['Date']);

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

发布评论

评论列表(0)

  1. 暂无评论