@@ -19,6 +19,7 @@ import { iso, serverUtils } from 'playwright-core/lib/coreBundle';
1919import { ExpectError , isJestError } from './matcherHint' ;
2020import {
2121 computeMatcherTitleSuffix ,
22+ defaultDeadlineForMatcher ,
2223 toBeAttached ,
2324 toBeChecked ,
2425 toBeDisabled ,
@@ -54,13 +55,47 @@ import { toHaveScreenshot, toMatchSnapshot } from './toMatchSnapshot';
5455import {
5556 expect as expectLibrary ,
5657} from '../common/expectBundle' ;
57- import { currentTestInfo } from '../common/globals' ;
5858import { filteredStackTrace } from '../util' ;
59- import { TestInfoImpl } from '../worker/testInfo' ;
6059
6160import type { ExpectMatcherStateInternal } from './matchers' ;
6261import type { Expect } from '../../types/test' ;
63- import type { TestStepInfoImpl } from '../worker/testInfo' ;
62+ import type { TestInfoImpl , TestStepInfoImpl } from '../worker/testInfo' ;
63+
64+ export type ExpectConfig = {
65+ testInfo ?: TestInfoImpl ;
66+ timeout ?: number ;
67+ toHaveScreenshot ?: {
68+ threshold ?: number ;
69+ maxDiffPixels ?: number ;
70+ maxDiffPixelRatio ?: number ;
71+ animations ?: 'allow' | 'disabled' ;
72+ caret ?: 'hide' | 'initial' ;
73+ scale ?: 'css' | 'device' ;
74+ stylePath ?: string | Array < string > ;
75+ pathTemplate ?: string ;
76+ } ;
77+ toMatchAriaSnapshot ?: {
78+ pathTemplate ?: string ;
79+ children ?: 'contain' | 'equal' | 'deep-equal' ;
80+ } ;
81+ toMatchSnapshot ?: {
82+ threshold ?: number ;
83+ maxDiffPixels ?: number ;
84+ maxDiffPixelRatio ?: number ;
85+ } ;
86+ toPass ?: {
87+ timeout ?: number ;
88+ intervals ?: Array < number > ;
89+ } ;
90+ } ;
91+
92+ let currentConfig : ExpectConfig = { } ;
93+ export function setExpectConfig ( config : ExpectConfig ) {
94+ currentConfig = config ;
95+ }
96+ export function expectConfig ( ) : ExpectConfig {
97+ return currentConfig ;
98+ }
6499
65100type ExpectMessage = string | { message ?: string } ;
66101
@@ -152,7 +187,6 @@ function createExpect(info: ExpectMetaInfo, prefix: string[], userMatchers: Reco
152187// Rely on sync call sequence to seed each matcher call with the context.
153188type MatcherCallContext = {
154189 expectInfo : ExpectMetaInfo ;
155- testInfo : TestInfoImpl | null ;
156190 step ?: TestStepInfoImpl ;
157191} ;
158192
@@ -178,7 +212,7 @@ function wrapPlaywrightMatcherToPassNiceThis(matcher: any) {
178212 return function ( this : any , ...args : any [ ] ) {
179213 const { isNot, promise, utils } = this ;
180214 const context = takeMatcherCallContext ( ) ;
181- const timeout = context ?. expectInfo . timeout ?? context ?. testInfo ?. _projectInternal ?. expect ? .timeout ?? defaultExpectTimeout ;
215+ const timeout = context ?. expectInfo . timeout ?? expectConfig ( ) . timeout ?? defaultExpectTimeout ;
182216 const newThis : ExpectMatcherStateInternal = {
183217 isNot,
184218 promise,
@@ -291,8 +325,8 @@ class ExpectMetaInfoProxyHandler implements ProxyHandler<any> {
291325 matcher = ( ...args : any [ ] ) => pollMatcher ( resolvedMatcherName , this . _info , this . _prefix , ...args ) ;
292326 }
293327 return ( ...args : any [ ] ) => {
294- const testInfo = currentTestInfo ( ) ;
295- setMatcherCallContext ( { expectInfo : this . _info , testInfo } ) ;
328+ const testInfo = expectConfig ( ) . testInfo ;
329+ setMatcherCallContext ( { expectInfo : this . _info } ) ;
296330 if ( ! testInfo )
297331 return matcher . call ( target , ...args ) ;
298332
@@ -344,7 +378,7 @@ class ExpectMetaInfoProxyHandler implements ProxyHandler<any> {
344378 } ;
345379
346380 try {
347- setMatcherCallContext ( { expectInfo : this . _info , testInfo , step : step . info } ) ;
381+ setMatcherCallContext ( { expectInfo : this . _info , step : step . info } ) ;
348382 const callback = ( ) => matcher . call ( target , ...args ) ;
349383 const result = serverUtils . currentZone ( ) . with ( 'stepZone' , step ) . run ( callback ) ;
350384 if ( result instanceof Promise )
@@ -359,13 +393,13 @@ class ExpectMetaInfoProxyHandler implements ProxyHandler<any> {
359393}
360394
361395async function pollMatcher ( qualifiedMatcherName : string , info : ExpectMetaInfo , prefix : string [ ] , ...args : any [ ] ) {
362- const testInfo = currentTestInfo ( ) ;
396+ const config = expectConfig ( ) ;
363397 const poll = info . poll ! ;
364- const timeout = poll . timeout ?? info . timeout ?? testInfo ?. _projectInternal ?. expect ? .timeout ?? defaultExpectTimeout ;
365- const { deadline, timeoutMessage } = testInfo ? testInfo . _deadlineForMatcher ( timeout ) : TestInfoImpl . _defaultDeadlineForMatcher ( timeout ) ;
398+ const timeout = poll . timeout ?? info . timeout ?? config . timeout ?? defaultExpectTimeout ;
399+ const { deadline, timeoutMessage } = config . testInfo ? config . testInfo . _deadlineForMatcher ( timeout ) : defaultDeadlineForMatcher ( timeout ) ;
366400
367401 const result = await iso . pollAgainstDeadline < Error | undefined > ( async ( ) => {
368- if ( testInfo && currentTestInfo ( ) !== testInfo )
402+ if ( expectConfig ( ) !== config )
369403 return { continuePolling : false , result : undefined } ;
370404
371405 const innerInfo : ExpectMetaInfo = {
0 commit comments