From c01379b9c9b3ff155a88a3342ec5171e226a3815 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Wed, 8 Jul 2026 10:11:06 +0200 Subject: [PATCH] test(clock): unflake "while running" page-clock tests --- tests/library/page-clock.spec.ts | 34 ++++++++++++++------------------ 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/tests/library/page-clock.spec.ts b/tests/library/page-clock.spec.ts index 8d653da944a69..c31693f38159c 100644 --- a/tests/library/page-clock.spec.ts +++ b/tests/library/page-clock.spec.ts @@ -399,66 +399,62 @@ it.describe('setFixedTime', () => { it.describe('while running', () => { it('should progress time', async ({ page }) => { await page.clock.install({ time: 0 }); + const startRealTime = Date.now(); await page.goto('data:text/html,'); await page.waitForTimeout(1000); const now = await page.evaluate(() => Date.now()); + const realElapsed = Date.now() - startRealTime; expect(now).toBeGreaterThanOrEqual(1000); - expect(now).toBeLessThanOrEqual(2000); + expect(now).toBeLessThanOrEqual(realElapsed + 1000); }); it('should runFor', async ({ page }) => { await page.clock.install({ time: 0 }); + const startRealTime = Date.now(); await page.goto('data:text/html,'); await page.clock.runFor(10000); const now = await page.evaluate(() => Date.now()); + const realElapsed = Date.now() - startRealTime; expect(now).toBeGreaterThanOrEqual(10000); - expect(now).toBeLessThanOrEqual(11000); + expect(now).toBeLessThanOrEqual(10000 + realElapsed + 1000); }); it('should fastForward', async ({ page }) => { await page.clock.install({ time: 0 }); + const startRealTime = Date.now(); await page.goto('data:text/html,'); await page.clock.fastForward(10000); const now = await page.evaluate(() => Date.now()); + const realElapsed = Date.now() - startRealTime; expect(now).toBeGreaterThanOrEqual(10000); - expect(now).toBeLessThanOrEqual(11000); - }); - - it('should fastForwardTo', async ({ page }) => { - await page.clock.install({ time: 0 }); - await page.goto('data:text/html,'); - await page.clock.fastForward(10000); - const now = await page.evaluate(() => Date.now()); - expect(now).toBeGreaterThanOrEqual(10000); - expect(now).toBeLessThanOrEqual(11000); + expect(now).toBeLessThanOrEqual(10000 + realElapsed + 1000); }); it('should pause', async ({ page }) => { await page.clock.install({ time: 0 }); await page.goto('data:text/html,'); - await page.clock.pauseAt(1000); + await page.clock.pauseAt(60000); // Internally wait to make sure the clock is paused and not running. await page.waitForTimeout(1111); const now = await page.evaluate(() => Date.now()); - expect(now).toBeGreaterThanOrEqual(0); - expect(now).toBeLessThanOrEqual(1000); + expect(now).toBe(60000); }); it('should pause and fastForward', async ({ page }) => { await page.clock.install({ time: 0 }); await page.goto('data:text/html,'); - await page.clock.pauseAt(1000); + await page.clock.pauseAt(60000); await page.clock.fastForward(1000); const now = await page.evaluate(() => Date.now()); - expect(now).toBe(2000); + expect(now).toBe(61000); }); it('should set system time on pause', async ({ page }) => { await page.clock.install({ time: 0 }); await page.goto('data:text/html,'); - await page.clock.pauseAt(1000); + await page.clock.pauseAt(60000); const now = await page.evaluate(() => Date.now()); - expect(now).toBe(1000); + expect(now).toBe(60000); }); });