fix(clock): don't throw for |null| or |undefined| callbacks (#32309)

Fixes https://github.com/microsoft/playwright/issues/32293

This aligns it how Chromium and other browsers are doing it.
This commit is contained in:
Max Schmitt 2024-08-26 18:26:38 +02:00 committed by GitHub
parent 54709880c2
commit 67d3d5f203
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 1 deletions

View File

@ -239,7 +239,12 @@ export class ClockController {
addTimer(options: { func: TimerHandler, type: TimerType, delay?: number | string, args?: any[] }): number { addTimer(options: { func: TimerHandler, type: TimerType, delay?: number | string, args?: any[] }): number {
this._replayLogOnce(); this._replayLogOnce();
if (options.func === undefined)
if (options.type === TimerType.AnimationFrame && !options.func)
throw new Error('Callback must be provided to requestAnimationFrame calls');
if (options.type === TimerType.IdleCallback && !options.func)
throw new Error('Callback must be provided to requestIdleCallback calls');
if ([TimerType.Timeout, TimerType.Interval].includes(options.type) && !options.func && options.delay === undefined)
throw new Error('Callback must be provided to timer calls'); throw new Error('Callback must be provided to timer calls');
let delay = options.delay ? +options.delay : 0; let delay = options.delay ? +options.delay : 0;

View File

@ -75,6 +75,14 @@ it.describe('setTimeout', () => {
}).toThrow(); }).toThrow();
}); });
it('does not throw if |undefined| or |null| is passed as a callback', async ({ clock }) => {
const timerId1 = clock.setTimeout(undefined, 10);
const timerId2 = clock.setTimeout(null, 10);
await clock.runFor(10);
expect(timerId1).toBeGreaterThan(0);
expect(timerId2).toBeGreaterThan(timerId1);
});
it('returns numeric id or object with numeric id', async ({ clock }) => { it('returns numeric id or object with numeric id', async ({ clock }) => {
const result = clock.setTimeout(() => { }, 10); const result = clock.setTimeout(() => { }, 10);
expect(result).toEqual(expect.any(Number)); expect(result).toEqual(expect.any(Number));
@ -761,6 +769,14 @@ it.describe('setInterval', () => {
}).toThrow(); }).toThrow();
}); });
it('does not throw if |undefined| or |null| is passed as a callback', async ({ clock }) => {
const timerId1 = clock.setInterval(undefined, 10);
const timerId2 = clock.setInterval(null, 10);
await clock.runFor(10);
expect(timerId1).toBeGreaterThan(0);
expect(timerId2).toBeGreaterThan(timerId1);
});
it('returns numeric id or object with numeric id', async ({ clock }) => { it('returns numeric id or object with numeric id', async ({ clock }) => {
const result = clock.setInterval(() => {}, 10); const result = clock.setInterval(() => {}, 10);
expect(result).toBeGreaterThan(0); expect(result).toBeGreaterThan(0);