fix(clock): ensure Date.now() is an integer (#31648)
Fixes https://github.com/microsoft/playwright/issues/31644
This commit is contained in:
parent
3127571b24
commit
297143885a
|
|
@ -53,7 +53,7 @@ type Timer = {
|
|||
|
||||
interface Embedder {
|
||||
dateNow(): number;
|
||||
performanceNow(): DOMHighResTimeStamp;
|
||||
performanceNow(): EmbedderTicks;
|
||||
setTimeout(task: () => void, timeout?: number): () => void;
|
||||
setInterval(task: () => void, delay: number): () => void;
|
||||
}
|
||||
|
|
@ -147,6 +147,8 @@ export class ClockController {
|
|||
}
|
||||
|
||||
private async _runTo(to: Ticks) {
|
||||
to = Math.ceil(to) as Ticks;
|
||||
|
||||
if (this._now.ticks > to)
|
||||
return;
|
||||
|
||||
|
|
@ -182,7 +184,7 @@ export class ClockController {
|
|||
}
|
||||
|
||||
private _innerResume() {
|
||||
const now = this._embedder.performanceNow() as EmbedderTicks;
|
||||
const now = this._embedder.performanceNow();
|
||||
this._realTime = { startTicks: now, lastSyncTicks: now };
|
||||
this._updateRealTimeTimer();
|
||||
}
|
||||
|
|
@ -209,7 +211,7 @@ export class ClockController {
|
|||
this._currentRealTimeTimer = {
|
||||
callAt,
|
||||
dispose: this._embedder.setTimeout(() => {
|
||||
const now = Math.ceil(this._embedder.performanceNow()) as EmbedderTicks;
|
||||
const now = this._embedder.performanceNow();
|
||||
this._currentRealTimeTimer = undefined;
|
||||
const sinceLastSync = now - this._realTime!.lastSyncTicks;
|
||||
this._realTime!.lastSyncTicks = now;
|
||||
|
|
@ -662,7 +664,7 @@ export function createClock(globalObject: WindowOrWorkerGlobalScope): { clock: C
|
|||
const originals = platformOriginals(globalObject);
|
||||
const embedder: Embedder = {
|
||||
dateNow: () => originals.raw.Date.now(),
|
||||
performanceNow: () => originals.raw.performance!.now(),
|
||||
performanceNow: () => Math.ceil(originals.raw.performance!.now()) as EmbedderTicks,
|
||||
setTimeout: (task: () => void, timeout?: number) => {
|
||||
const timerId = originals.bound.setTimeout(task, timeout);
|
||||
return () => originals.bound.clearTimeout(timerId);
|
||||
|
|
|
|||
|
|
@ -510,3 +510,25 @@ it.describe('while on pause', () => {
|
|||
expect(calls).toEqual([{ params: ['outer'] }, { params: ['inner'] }]);
|
||||
});
|
||||
});
|
||||
|
||||
it.describe('Date.now', () => {
|
||||
it('check Date.now is an integer', async ({ page }) => {
|
||||
await page.clock.install();
|
||||
await page.goto('data:text/html,');
|
||||
await page.waitForTimeout(1000);
|
||||
const dateValue = await page.evaluate('Date.now()');
|
||||
expect(Number.isInteger(dateValue)).toBeTruthy();
|
||||
await page.waitForTimeout(1000);
|
||||
const dateValue2 = await page.evaluate('Date.now()');
|
||||
expect(Number.isInteger(dateValue2)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('check Date.now is an integer (2)', async ({ page }) => {
|
||||
await page.clock.install({ time: 0 });
|
||||
await page.goto('data:text/html,');
|
||||
await page.clock.pauseAt(1000);
|
||||
await page.clock.runFor(0.5);
|
||||
const dateValue = await page.evaluate('Date.now()');
|
||||
expect(dateValue).toBe(1001);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue