@@ -11,14 +11,40 @@ const domLibraryAsString = readFileSync(
1111) . replace ( / p r o c e s s .e n v / g, '{}' )
1212
1313/* istanbul ignore next */
14- function mapArgument ( argument : any , index : number ) : any {
15- return index === 0 && typeof argument === 'object' && argument . regex
16- ? new RegExp ( argument . regex , argument . flags )
17- : argument
14+ function convertProxyToRegExp ( o : any , depth : number ) : any {
15+ if ( typeof o !== 'object' || ! o || depth > 2 ) return o
16+ if ( ! o . __regex || typeof o . __flags !== 'string' ) {
17+ const copy = { ...o }
18+ for ( const key of Object . keys ( copy ) ) {
19+ copy [ key ] = convertProxyToRegExp ( copy [ key ] , depth + 1 )
20+ }
21+ return copy
22+ }
23+
24+ return new RegExp ( o . __regex , o . __flags )
25+ }
26+
27+ /* istanbul ignore next */
28+ function mapArgument ( o : any ) : any {
29+ return convertProxyToRegExp ( o , 0 )
30+ }
31+
32+ function convertRegExpToProxy ( o : any , depth : number ) : any {
33+ if ( typeof o !== 'object' || ! o || depth > 2 ) return o
34+ if ( ! ( o instanceof RegExp ) ) {
35+ const copy = { ...o }
36+ for ( const key of Object . keys ( copy ) ) {
37+ copy [ key ] = convertRegExpToProxy ( copy [ key ] , depth + 1 )
38+ }
39+ return copy
40+ }
41+
42+ return { __regex : o . source , __flags : o . flags }
1843}
1944
2045const delegateFnBodyToExecuteInPageInitial = `
2146 ${ domLibraryAsString } ;
47+ ${ convertProxyToRegExp . toString ( ) } ;
2248
2349 const mappedArgs = args.map(${ mapArgument . toString ( ) } );
2450 const moduleWithFns = fnName in __dom_testing_library__ ?
@@ -97,8 +123,6 @@ function createDelegateFor<T = DOMReturnType>(
97123 // eslint-disable-next-line no-param-reassign
98124 processHandleFn = processHandleFn || processQuery
99125
100- const convertRegExp = ( regex : RegExp ) => ( { regex : regex . source , flags : regex . flags } )
101-
102126 return async function delegate ( ...args : any [ ] ) : Promise < T > {
103127 // @ts -ignore
104128 const containerHandle : ElementHandle = contextFn ? contextFn . apply ( this , args ) : this
@@ -107,13 +131,15 @@ function createDelegateFor<T = DOMReturnType>(
107131 // eslint-disable-next-line no-new-func, @typescript-eslint/no-implied-eval
108132 const evaluateFn = new Function ( 'container, [fnName, ...args]' , delegateFnBodyToExecuteInPage )
109133
110- // Convert RegExp to a special format since they don't serialize well
111- let argsToForward = args . map ( arg => ( arg instanceof RegExp ? convertRegExp ( arg ) : arg ) )
134+ let argsToForward = args
112135 // Remove the container from the argsToForward since it's always the first argument
113136 if ( containerHandle === args [ 0 ] ) {
114137 argsToForward = argsToForward . slice ( 1 )
115138 }
116139
140+ // Convert RegExp to a special format since they don't serialize well
141+ argsToForward = argsToForward . map ( convertRegExpToProxy )
142+
117143 return processHandleFn ! ( { fnName, containerHandle, evaluateFn, argsToForward} )
118144 }
119145}
0 commit comments