microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
65.68k stars 3.57k forks source link

[Feature]: clock feature inside tests #32486

Closed Twiggeh closed 5 days ago

Twiggeh commented 1 week ago

🚀 Feature Request

The clock should also be able to install into a test and control the timers for said test (not just the code that is being tested).

Example

*.test.js

async (test) => {
await test.clock.install(time);
for(let i = 0; i < 30; i++) {
  await test.clock.runFor(30);
  console.log(Date.now())
  // ...30 ...60 ...90 ..120
}

Motivation

When testing e.g. a fetcher library with deduplication & caching, I need time sensitive controls. It would make life easier to have those timers available inside the test.

Right now I have inside the test

setInterval(()=>{
  const return = await fetch();
  didDedupWork(return);
}, 50)

But setInterval isn't super accurate, so it becomes more difficult to e.g. have a dedup test, since I cannot really rely on iterations or actual time spent. This also extends, for me at least, when what version of the cache should've been returned.

pavelfeldman commented 1 week ago

I am not sure what you mean with your example, I don't understand how it is related to time. Are you doing API testing? Does your request contain information about time? Please be more specific.

dgozman commented 5 days ago

Closing because we lack the information to act on this issue. If you can provide more details, please file a new issue by filling the "Feature Request" template, and link to this one.