@@ -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 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 }
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__ ?
@@ -97,8 +123,6 @@ function createDelegateFor<T = DOMReturnType>(
97
123
// eslint-disable-next-line no-param-reassign
98
124
processHandleFn = processHandleFn || processQuery
99
125
100
- const convertRegExp = ( regex : RegExp ) => ( { regex : regex . source , flags : regex . flags } )
101
-
102
126
return async function delegate ( ...args : any [ ] ) : Promise < T > {
103
127
// @ts -ignore
104
128
const containerHandle : ElementHandle = contextFn ? contextFn . apply ( this , args ) : this
@@ -107,13 +131,15 @@ function createDelegateFor<T = DOMReturnType>(
107
131
// eslint-disable-next-line no-new-func, @typescript-eslint/no-implied-eval
108
132
const evaluateFn = new Function ( 'container, [fnName, ...args]' , delegateFnBodyToExecuteInPage )
109
133
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
112
135
// Remove the container from the argsToForward since it's always the first argument
113
136
if ( containerHandle === args [ 0 ] ) {
114
137
argsToForward = argsToForward . slice ( 1 )
115
138
}
116
139
140
+ // Convert RegExp to a special format since they don't serialize well
141
+ argsToForward = argsToForward . map ( convertRegExpToProxy )
142
+
117
143
return processHandleFn ! ( { fnName, containerHandle, evaluateFn, argsToForward} )
118
144
}
119
145
}
0 commit comments