chore: should not spill toPass between tests (#22473)

This commit is contained in:
Pavel Feldman 2023-04-19 14:45:58 -07:00 committed by GitHub
parent 3c495c5590
commit e7b9c08833
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 0 deletions

View File

@ -283,7 +283,12 @@ class ExpectMetaInfoProxyHandler implements ProxyHandler<any> {
}
async function pollMatcher(matcherName: any, isNot: boolean, pollIntervals: number[] | undefined, timeout: number, generator: () => any, ...args: any[]) {
const testInfo = currentTestInfo();
const result = await pollAgainstTimeout<Error|undefined>(async () => {
if (testInfo && currentTestInfo() !== testInfo)
return { continuePolling: false, result: undefined };
const value = await generator();
let expectInstance = expectLibrary(value) as any;
if (isNot)

View File

@ -23,6 +23,7 @@ import { toBeTruthy } from './toBeTruthy';
import { toEqual } from './toEqual';
import { toExpectedTextValues, toMatchText } from './toMatchText';
import { constructURLBasedOnBaseURL, isTextualMimeType, pollAgainstTimeout } from 'playwright-core/lib/utils';
import { currentTestInfo } from '../common/globals';
interface LocatorEx extends Locator {
_expect(expression: string, options: Omit<FrameExpectOptions, 'expectedValue'> & { expectedValue?: any }): Promise<{ matches: boolean, received?: any, log?: string[], timedOut?: boolean }>;
@ -337,9 +338,12 @@ export async function toPass(
timeout?: number,
} = {},
) {
const testInfo = currentTestInfo();
const timeout = options.timeout !== undefined ? options.timeout : 0;
const result = await pollAgainstTimeout<Error|undefined>(async () => {
if (testInfo && currentTestInfo() !== testInfo)
return { continuePolling: false, result: undefined };
try {
await callback();
return { continuePolling: this.isNot, result: undefined };

View File

@ -182,3 +182,25 @@ test('should not accept TimeoutError', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(1);
expect(result.failed).toBe(1);
});
test('should not spin forever', async ({ runInlineTest }) => {
const result = await runInlineTest({
'a.spec.ts': `
import { test, expect } from '@playwright/test';
let log;
test('spill toPass', async () => {
expect(() => {
log?.push('poll');
throw new Error('Polling');
}).toPass().catch();
});
test('should not see toPass', async () => {
log = [];
await new Promise(f => setTimeout(f, 1000));
expect(log.length).toBe(0);
});
`
});
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(2);
});