Skip to content

Commit 08aebc7

Browse files
fix(types): add types for waitForEvent (#1601)
1 parent 13a6c89 commit 08aebc7

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

utils/generate_types/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ function createEventDescriptions(classDesc) {
141141
const argName = argNameForType(type);
142142
const params = argName ? `${argName} : ${type}` : '';
143143
descriptions.push({
144+
type,
144145
params,
145146
eventName,
146147
comment: value.comment
@@ -167,6 +168,16 @@ function classBody(classDesc) {
167168
parts.push(members.map(member => {
168169
if (member.kind === 'event')
169170
return '';
171+
if (member.name === 'waitForEvent') {
172+
const parts = [];
173+
for (const {eventName, params, comment, type} of eventDescriptions) {
174+
if (comment)
175+
parts.push(writeComment(comment, ' '));
176+
parts.push(` ${member.name}(event: '${eventName}', optionsOrPredicate?: { predicate?: (${params}) => boolean, timeout?: number }): Promise<${type}>;\n`);
177+
}
178+
179+
return parts.join('\n');
180+
}
170181
const jsdoc = memberJSDOC(member, ' ');
171182
const args = argsFromMember(member, classDesc.name);
172183
const type = typeToString(member.type, classDesc.name, member.name);

utils/generate_types/test/test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,31 @@ playwright.chromium.launch().then(async browser => {
290290
browser.close();
291291
})();
292292

293+
// waitForEvent
294+
(async () => {
295+
const browser = await playwright.webkit.launch();
296+
const page = await browser.newPage();
297+
{
298+
const frame = await page.waitForEvent('frameattached');
299+
const assertion: AssertType<playwright.Frame, typeof frame> = true;
300+
}
301+
{
302+
const worker = await page.waitForEvent('worker', {
303+
predicate: worker => {
304+
const condition: AssertType<playwright.Worker, typeof worker> = true;
305+
return true;
306+
}
307+
});
308+
const assertion: AssertType<playwright.Worker, typeof worker> = true;
309+
}
310+
{
311+
const newPage = await page.context().waitForEvent('page', {
312+
timeout: 500
313+
});
314+
const assertion: AssertType<playwright.Page, typeof newPage> = true;
315+
}
316+
})();
317+
293318
// typed handles
294319
(async () => {
295320
const browser = await playwright.webkit.launch();
@@ -515,7 +540,6 @@ playwright.chromium.launch().then(async browser => {
515540
}
516541
}
517542

518-
519543
await browser.close();
520544
})();
521545

0 commit comments

Comments
 (0)