From 599d074f7176d353ce2fdb7346a1bc3b047a8d05 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Wed, 19 Jun 2024 09:26:47 +0200 Subject: [PATCH] cherry-pick(#31356): fix(clock): throw for invalid date Fixes https://github.com/microsoft/playwright/issues/31354 --- packages/playwright-core/src/client/clock.ts | 2 ++ packages/playwright-core/src/server/clock.ts | 5 ++++- tests/page/page-clock.spec.ts | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/playwright-core/src/client/clock.ts b/packages/playwright-core/src/client/clock.ts index 32faa7a4d4f2e..73eb538c264bf 100644 --- a/packages/playwright-core/src/client/clock.ts +++ b/packages/playwright-core/src/client/clock.ts @@ -58,6 +58,8 @@ function parseTime(time: string | number | Date): { timeNumber?: number, timeStr return { timeNumber: time }; if (typeof time === 'string') return { timeString: time }; + if (!isFinite(time.getTime())) + throw new Error(`Invalid date: ${time}`); return { timeNumber: time.getTime() }; } diff --git a/packages/playwright-core/src/server/clock.ts b/packages/playwright-core/src/server/clock.ts index 1e0822ff04b2d..5049e4766d9f3 100644 --- a/packages/playwright-core/src/server/clock.ts +++ b/packages/playwright-core/src/server/clock.ts @@ -148,5 +148,8 @@ function parseTime(epoch: string | number | undefined): number { return 0; if (typeof epoch === 'number') return epoch; - return new Date(epoch).getTime(); + const parsed = new Date(epoch); + if (!isFinite(parsed.getTime())) + throw new Error(`Invalid date: ${epoch}`); + return parsed.getTime(); } diff --git a/tests/page/page-clock.spec.ts b/tests/page/page-clock.spec.ts index 670891a40a229..29854c969c9b9 100644 --- a/tests/page/page-clock.spec.ts +++ b/tests/page/page-clock.spec.ts @@ -245,6 +245,11 @@ it.describe('stubTimers', () => { expect(await page.evaluate(() => Date.now())).toBe(1400); }); + it('should throw for invalid date', async ({ page }) => { + await expect(page.clock.setSystemTime(new Date('invalid'))).rejects.toThrow('Invalid date: Invalid Date'); + await expect(page.clock.setSystemTime('invalid')).rejects.toThrow('clock.setSystemTime: Invalid date: invalid'); + }); + it('replaces global setTimeout', async ({ page, calls }) => { await page.evaluate(async () => { setTimeout(window.stub, 1000);