From 82f7fadba33b8fc26aae8d0c25f8c3e21850b8a8 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Tue, 18 Jun 2024 11:54:49 +0200 Subject: [PATCH] fix: throw for invalid date --- 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 58a1f7272038e..6aa321025d38c 100644 --- a/packages/playwright-core/src/server/clock.ts +++ b/packages/playwright-core/src/server/clock.ts @@ -144,5 +144,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 220f4725fcf41..7096af0db1f0b 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);