Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions packages/injected/src/clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,11 @@ function platformOriginals(globalObject: WindowOrWorkerGlobalScope): { raw: Buil
Date: (globalObject as any).Date,
performance: globalObject.performance,
Intl: (globalObject as any).Intl,
AbortSignal: (globalObject as any).AbortSignal,
};
const bound = { ...raw };
for (const key of Object.keys(bound) as (keyof Builtins)[]) {
if (key !== 'Date' && typeof bound[key] === 'function')
if (key !== 'Date' && key !== 'AbortSignal' && typeof bound[key] === 'function')
bound[key] = (bound[key] as any).bind(globalObject);
}
return { raw, bound };
Expand All @@ -632,16 +633,17 @@ function getScheduleHandler(type: TimerType) {
}

function createApi(clock: ClockController, originals: Builtins): Builtins {
const setTimeout = (func: TimerHandler, timeout?: number | undefined, ...args: any[]) => {
const delay = timeout ? +timeout : timeout;
return clock.addTimer({
type: TimerType.Timeout,
func,
args,
delay
});
};
return {
setTimeout: (func: TimerHandler, timeout?: number | undefined, ...args: any[]) => {
const delay = timeout ? +timeout : timeout;
return clock.addTimer({
type: TimerType.Timeout,
func,
args,
delay
});
},
setTimeout,
clearTimeout: (timerId: number | undefined): void => {
if (timerId)
clock.clearTimer(timerId, TimerType.Timeout);
Expand Down Expand Up @@ -688,6 +690,7 @@ function createApi(clock: ClockController, originals: Builtins): Builtins {
Intl: originals.Intl ? createIntl(clock, originals.Intl) : (undefined as unknown as Builtins['Intl']),
Date: createDate(clock, originals.Date),
performance: originals.performance ? fakePerformance(clock, originals.performance) : (undefined as unknown as Builtins['performance']),
AbortSignal: originals.AbortSignal ? fakeAbortSignal(originals.AbortSignal, setTimeout) : originals.AbortSignal,
};
}

Expand Down Expand Up @@ -715,6 +718,15 @@ function fakePerformance(clock: ClockController, performance: Builtins['performa
return result;
}

function fakeAbortSignal(NativeAbortSignal: Builtins['AbortSignal'], fakeSetTimeout: Builtins['setTimeout']): Builtins['AbortSignal'] {
(NativeAbortSignal as any).timeout = function(ms: number): AbortSignal {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is mutating the existing global, that's bad!

const controller = new AbortController();
fakeSetTimeout(() => controller.abort(), ms);
return controller.signal;
};
return NativeAbortSignal;
}

export function createClock(globalObject: WindowOrWorkerGlobalScope): { clock: ClockController, api: Builtins, originals: Builtins } {
const originals = platformOriginals(globalObject);
const embedder: Embedder = {
Expand Down Expand Up @@ -760,6 +772,8 @@ export function install(globalObject: WindowOrWorkerGlobalScope, config: Install
return this[kEventTimeStamp];
}
});
} else if (method === 'AbortSignal') {
(globalObject as any).AbortSignal = api[method]!;
} else {
(globalObject as any)[method] = (...args: any[]) => {
return (api[method] as any).apply(api, args);
Expand Down
2 changes: 2 additions & 0 deletions packages/injected/src/utilityScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type Builtins = {
performance: Window['performance'],
Intl: typeof window['Intl'],
Date: typeof window['Date'],
AbortSignal: typeof window['AbortSignal'],
};

export class UtilityScript {
Expand All @@ -55,6 +56,7 @@ export class UtilityScript {
performance: global.performance,
Intl: global.Intl,
Date: global.Date,
AbortSignal: global.AbortSignal,
} satisfies Builtins;
}
if (this.isUnderTest)
Expand Down
95 changes: 95 additions & 0 deletions tests/library/page-clock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,3 +565,98 @@ it('correctly increments Date.now()/performance.now() during blocking execution'
await page.goto(server.PREFIX + '/repro.html');
await waitForDone;
});

it.describe('AbortSignal.timeout', () => {
it('should abort signal after timeout', async ({ page }) => {
await page.clock.install({ time: 0 });

const signalHandle = await page.evaluateHandle(() => {
return AbortSignal.timeout(5000);
});

// Check initial state
expect(await signalHandle.evaluate(s => s.aborted)).toBe(false);

// Fast forward past the timeout
await page.clock.runFor(6000);

// Check that signal is now aborted
expect(await signalHandle.evaluate(s => s.aborted)).toBe(true);
});

it('should fire addEventListener', async ({ page }) => {
await page.clock.install({ time: 0 });

const result = await page.evaluate(() => {
return new Promise<boolean>(resolve => {
const signal = AbortSignal.timeout(5000);
let didAbort = false;
signal.addEventListener('abort', () => {
didAbort = true;
resolve(didAbort);
});
// If the signal doesn't abort, resolve after a longer time
setTimeout(() => resolve(didAbort), 10000);
});
});

// Run for the timeout duration
await page.clock.runFor(6000);

expect(result).toBe(true);
});

it('should fire onabort handler', async ({ page }) => {
await page.clock.install({ time: 0 });

const result = await page.evaluate(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, i don't think this test will work. won't this be waiting forever for the setTimeout call to resolve? i think you can move this all into the first test with "evaluateHandle"!

return new Promise<boolean>(resolve => {
const signal = AbortSignal.timeout(5000);
let didAbort = false;
signal.onabort = () => {
didAbort = true;
resolve(didAbort);
};
// If the signal doesn't abort, resolve after a longer time
setTimeout(() => resolve(didAbort), 10000);
});
});

// Run for the timeout duration
await page.clock.runFor(6000);

expect(result).toBe(true);
});

it('should work with fastForward', async ({ page }) => {
await page.clock.install({ time: 0 });

const signalHandle = await page.evaluateHandle(() => {
return AbortSignal.timeout(5000);
});

expect(await signalHandle.evaluate(s => s.aborted)).toBe(false);

await page.clock.fastForward(6000);

expect(await signalHandle.evaluate(s => s.aborted)).toBe(true);
});

it('should work with multiple signals', async ({ page }) => {
await page.clock.install({ time: 0 });

const result = await page.evaluate(() => {
const results: number[] = [];
AbortSignal.timeout(1000).addEventListener('abort', () => results.push(1));
AbortSignal.timeout(2000).addEventListener('abort', () => results.push(2));
AbortSignal.timeout(3000).addEventListener('abort', () => results.push(3));
return new Promise<number[]>(resolve => {
setTimeout(() => resolve(results), 5000);
});
});

await page.clock.runFor(5000);

expect(result).toEqual([1, 2, 3]);
});
});