Skip to content

Commit eafa6fd

Browse files
authored
fix(expect): poll/toPass should not wait over specified timeout (#20266)
Drive-by: unflake some timeout-dependent tests.
1 parent d8e8ddb commit eafa6fd

5 files changed

Lines changed: 19 additions & 10 deletions

File tree

packages/playwright-core/src/utils/timeoutRunner.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,12 @@ export async function pollAgainstTimeout<T>(callback: () => Promise<{ continuePo
126126
lastResult = received.result.result;
127127
if (!received.result.continuePolling)
128128
return { result: received.result.result, timedOut: false };
129-
await new Promise(x => setTimeout(x, pollIntervals!.shift() ?? lastPollInterval));
129+
let interval = pollIntervals!.shift() ?? lastPollInterval;
130+
if (timeout !== 0) {
131+
const elapsed = monotonicTime() - startTime;
132+
interval = Math.min(interval, Math.max(timeout - elapsed, 1));
133+
}
134+
await new Promise(x => setTimeout(x, interval));
130135
}
131136
return { timedOut: true, result: lastResult };
132137
}

packages/playwright-test/src/matchers/matchers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ export async function toBeOK(
311311

312312
export async function toPass(
313313
this: ReturnType<Expect['getState']>,
314-
callback: () => void,
314+
callback: () => any,
315315
options: {
316316
intervals?: number[];
317317
timeout?: number,

tests/library/global-fetch.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ it('should support global userAgent option', async ({ playwright, server }) => {
5656
});
5757

5858
it('should support global timeout option', async ({ playwright, server }) => {
59-
const request = await playwright.request.newContext({ timeout: 1 });
59+
const request = await playwright.request.newContext({ timeout: 100 });
6060
server.setRoute('/empty.html', (req, res) => {});
6161
const error = await request.get(server.EMPTY_PAGE).catch(e => e);
62-
expect(error.message).toContain('Request timed out after 1ms');
62+
expect(error.message).toContain('Request timed out after 100ms');
6363
});
6464

6565
it('should propagate extra http headers with redirects', async ({ playwright, server }) => {

tests/playwright-test/expect-poll.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,11 @@ test('should respect interval', async ({ runInlineTest }) => {
208208
const { test } = pwt;
209209
test('should fail', async () => {
210210
let probes = 0;
211-
await test.expect.poll(() => ++probes, { timeout: 1000, intervals: [600] }).toBe(3).catch(() => {});
212-
// Probe at 0s, at 0.6s.
211+
const startTime = Date.now();
212+
await test.expect.poll(() => ++probes, { timeout: 1000, intervals: [0, 10000] }).toBe(3).catch(() => {});
213+
// Probe at 0 and epsilon.
213214
expect(probes).toBe(2);
215+
expect(Date.now() - startTime).toBeLessThan(5000);
214216
});
215217
`
216218
});

tests/playwright-test/expect-to-pass.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,14 @@ test('should respect interval', async ({ runInlineTest }) => {
100100
const { test } = pwt;
101101
test('should fail', async () => {
102102
let probes = 0;
103+
const startTime = Date.now();
103104
await test.expect(() => {
104-
++probes
105+
++probes;
105106
expect(1).toBe(2);
106-
}).toPass({ timeout: 1000, intervals: [600] }).catch(() => {});
107-
// Probe at 0s, at 0.6s.
107+
}).toPass({ timeout: 1000, intervals: [0, 10000] }).catch(() => {});
108+
// Probe at 0 and epsilon.
108109
expect(probes).toBe(2);
110+
expect(Date.now() - startTime).toBeLessThan(5000);
109111
});
110112
`
111113
});
@@ -157,7 +159,7 @@ test('should work with soft', async ({ runInlineTest }) => {
157159
test('should respect soft', async () => {
158160
await test.expect.soft(() => {
159161
expect(1).toBe(3);
160-
}).toPass({ timeout: 1 });
162+
}).toPass({ timeout: 1000 });
161163
expect.soft(2).toBe(3);
162164
});
163165
`

0 commit comments

Comments
 (0)