@@ -11,14 +11,40 @@ const domLibraryAsString = readFileSync(
11
11
) . replace ( / p r o c e s s .e n v / g, '{}' )
12
12
13
13
/* 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 mapArgument ( o : any ) : any {
15
+ return convertProxyToRegExp ( o , 0 )
16
+ }
17
+
18
+ /* istanbul ignore next */
19
+ function convertProxyToRegExp ( o : any , depth : number ) : any {
20
+ if ( typeof o !== 'object' || ! o || depth > 2 ) return o
21
+ if ( ! o . __regex || typeof o . __flags !== 'string' ) {
22
+ const copy = { ...o }
23
+ for ( const key of Object . keys ( copy ) ) {
24
+ copy [ key ] = convertProxyToRegExp ( copy [ key ] , depth + 1 )
25
+ }
26
+ return copy
27
+ }
28
+
29
+ return new RegExp ( o . __regex , o . __flags )
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 }
18
43
}
19
44
20
45
const delegateFnBodyToExecuteInPageInitial = `
21
46
${ domLibraryAsString } ;
47
+ ${ convertProxyToRegExp . toString ( ) } ;
22
48
23
49
const mappedArgs = args.map(${ mapArgument . toString ( ) } );
24
50
const moduleWithFns = fnName in __dom_testing_library__ ?
@@ -94,8 +120,6 @@ function createDelegateFor<T = DOMReturnType>(
94
120
// @ts -ignore
95
121
processHandleFn = processHandleFn || processQuery
96
122
97
- const convertRegExp = ( regex : RegExp ) => ( { regex : regex . source , flags : regex . flags } )
98
-
99
123
return async function ( ...args : any [ ] ) : Promise < T > {
100
124
// @ts -ignore
101
125
const containerHandle : ElementHandle = contextFn ? contextFn . apply ( this , args ) : this
@@ -105,13 +129,15 @@ function createDelegateFor<T = DOMReturnType>(
105
129
delegateFnBodyToExecuteInPage ,
106
130
)
107
131
108
- // Convert RegExp to a special format since they don't serialize well
109
- let argsToForward = args . map ( arg => ( arg instanceof RegExp ? convertRegExp ( arg ) : arg ) )
132
+ let argsToForward = args
110
133
// Remove the container from the argsToForward since it's always the first argument
111
134
if ( containerHandle === args [ 0 ] ) {
112
135
argsToForward = argsToForward . slice ( 1 )
113
136
}
114
137
138
+ // Convert RegExp to a special format since they don't serialize well
139
+ argsToForward = argsToForward . map ( convertRegExpToProxy )
140
+
115
141
return processHandleFn ! ( { fnName, containerHandle, evaluateFn, argsToForward} )
116
142
}
117
143
}
0 commit comments