Skip to content

Commit

Permalink
cherry-pick(#31356): fix(clock): throw for invalid date
Browse files Browse the repository at this point in the history
Fixes #31354
  • Loading branch information
mxschmitt committed Jun 19, 2024
1 parent 2451791 commit 599d074
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/playwright-core/src/client/clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() };
}

Expand Down
5 changes: 4 additions & 1 deletion packages/playwright-core/src/server/clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
5 changes: 5 additions & 0 deletions tests/page/page-clock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 599d074

Please sign in to comment.