@@ -25,8 +25,7 @@ import { toEqual } from './toEqual';
2525import { toExpectedTextValues , toMatchText } from './toMatchText' ;
2626import type { ParsedStackTrace } from 'playwright-core/lib/utils/stackTrace' ;
2727import { isTextualMimeType } from 'playwright-core/lib/utils/mimeType' ;
28- import { monotonicTime } from 'playwright-core/lib/utils' ;
29- import { raceAgainstTimeout } from 'playwright-core/lib/utils/timeoutRunner' ;
28+ import { pollAgainstTimeout } from 'playwright-core/lib/utils/timeoutRunner' ;
3029
3130interface LocatorEx extends Locator {
3231 _expect ( customStackTrace : ParsedStackTrace , expression : string , options : Omit < FrameExpectOptions , 'expectedValue' > & { expectedValue ?: any } ) : Promise < { matches : boolean , received ?: any , log ?: string [ ] , timedOut ?: boolean } > ;
@@ -320,43 +319,27 @@ export async function toPass(
320319 timeout ?: number ,
321320 } = { } ,
322321) {
323- let matcherError : Error | undefined ;
324- const startTime = monotonicTime ( ) ;
325- const pollIntervals = options . intervals || [ 100 , 250 , 500 , 1000 ] ;
326- const lastPollInterval = pollIntervals [ pollIntervals . length - 1 ] || 1000 ;
327322 const timeout = options . timeout !== undefined ? options . timeout : 0 ;
328- const isNot = this . isNot ;
329323
330- while ( true ) {
331- const elapsed = monotonicTime ( ) - startTime ;
332- if ( timeout !== 0 && elapsed > timeout )
333- break ;
324+ const result = await pollAgainstTimeout < Error | undefined > ( async ( ) => {
334325 try {
335- const wrappedCallback = ( ) => Promise . resolve ( ) . then ( callback ) ;
336- const received = timeout !== 0 ? await raceAgainstTimeout ( wrappedCallback , timeout - elapsed )
337- : await wrappedCallback ( ) . then ( ( ) => ( { timedOut : false } ) ) ;
338- if ( received . timedOut )
339- break ;
340- // The check passed, exit sucessfully.
341- if ( isNot )
342- matcherError = new Error ( 'Expected to fail, but passed' ) ;
343- else
344- return { message : ( ) => '' , pass : true } ;
326+ await callback ( ) ;
327+ return { continuePolling : this . isNot , result : undefined } ;
345328 } catch ( e ) {
346- if ( isNot )
347- return { message : ( ) => '' , pass : false } ;
348- matcherError = e ;
329+ return { continuePolling : ! this . isNot , result : e } ;
349330 }
350- await new Promise ( x => setTimeout ( x , pollIntervals ! . shift ( ) ?? lastPollInterval ) ) ;
351- }
331+ } , timeout , options . intervals || [ 100 , 250 , 500 , 1000 ] ) ;
352332
353- const timeoutMessage = `Timeout ${ timeout } ms exceeded while waiting on the predicate` ;
354- const message = ( ) => matcherError ? [
355- matcherError . message ,
356- '' ,
357- `Call Log:` ,
358- `- ${ timeoutMessage } ` ,
359- ] . join ( '\n' ) : timeoutMessage ;
333+ if ( result . timedOut ) {
334+ const timeoutMessage = `Timeout ${ timeout } ms exceeded while waiting on the predicate` ;
335+ const message = ( ) => result . result ? [
336+ result . result . message ,
337+ '' ,
338+ `Call Log:` ,
339+ `- ${ timeoutMessage } ` ,
340+ ] . join ( '\n' ) : timeoutMessage ;
360341
361- return { message, pass : isNot ? true : false } ;
362- }
342+ return { message, pass : this . isNot } ;
343+ }
344+ return { pass : ! this . isNot , message : ( ) => '' } ;
345+ }
0 commit comments