1- const path = require ( 'path' )
2- const fs = require ( 'fs' )
3- const { queries : baseQueries } = require ( '@testing-library/dom' )
1+ /* eslint-disable @typescript-eslint/no-implied-eval babel/no-invalid-this */
2+
3+ import path from 'path'
4+ import fs from 'fs'
5+ import { queries as baseQueries } from '@testing-library/dom'
6+ import { Element , BrowserObject , MultiRemoteBrowserObject } from 'webdriverio'
7+
8+ import { Config , QueryName , WebdriverIOQueries } from './types'
9+
10+ declare global {
11+ interface Window {
12+ TestingLibraryDom : typeof baseQueries & {
13+ configure : typeof configure
14+ }
15+ }
16+ }
417
518const DOM_TESTING_LIBRARY_UMD_PATH = path . join (
619 require . resolve ( '@testing-library/dom' ) ,
@@ -11,9 +24,9 @@ const DOM_TESTING_LIBRARY_UMD = fs
1124 . readFileSync ( DOM_TESTING_LIBRARY_UMD_PATH )
1225 . toString ( )
1326
14- let _config
27+ let _config : Partial < Config >
1528
16- async function injectDOMTestingLibrary ( container ) {
29+ async function injectDOMTestingLibrary ( container : Element ) {
1730 await container . execute ( DOM_TESTING_LIBRARY_UMD )
1831
1932 if ( _config ) {
@@ -23,7 +36,7 @@ async function injectDOMTestingLibrary(container) {
2336 }
2437}
2538
26- function serializeArgs ( args ) {
39+ function serializeArgs ( args : any [ ] ) {
2740 return args . map ( ( arg ) => {
2841 if ( arg instanceof RegExp ) {
2942 return { RegExp : arg . toString ( ) }
@@ -35,10 +48,12 @@ function serializeArgs(args) {
3548 } )
3649}
3750
38- function executeQuery ( [ query , container , ...args ] , done ) {
39- const deserializedArgs = args . map ( ( arg ) => {
51+ function executeQuery (
52+ [ query , container , ...args ] : [ QueryName , HTMLElement , ...any [ ] ] ,
53+ done : ( result : any ) => void ,
54+ ) {
55+ const [ matcher , options , waitForOptions ] = args . map ( ( arg ) => {
4056 if ( arg && arg . RegExp ) {
41- // eslint-disable-next-line
4257 return eval ( arg . RegExp )
4358 }
4459 if ( arg && arg . Undefined ) {
@@ -48,7 +63,12 @@ function executeQuery([query, container, ...args], done) {
4863 } )
4964
5065 Promise . resolve (
51- window . TestingLibraryDom [ query ] ( container , ...deserializedArgs ) ,
66+ window . TestingLibraryDom [ query ] (
67+ container ,
68+ matcher ,
69+ options ,
70+ waitForOptions ,
71+ ) ,
5272 )
5373 . then ( done )
5474 . catch ( ( e ) => done ( e . message ) )
@@ -62,18 +82,18 @@ Element. There are valid WebElement JSONs that exclude the key but can be turned
6282into Elements, such as { ELEMENT: elementId }; this can happen in setups that
6383aren't generated by @wdio/cli.
6484*/
65- function createElement ( container , elementValue ) {
85+ function createElement ( container : Element , elementValue : any ) {
6686 return container . $ ( {
6787 'element-6066-11e4-a52e-4f735466cecf' : '' ,
6888 ...elementValue ,
6989 } )
7090}
7191
72- function createQuery ( element , queryName ) {
73- return async ( ...args ) => {
92+ function createQuery ( element : Element , queryName : string ) {
93+ return async ( ...args : any [ ] ) => {
7494 await injectDOMTestingLibrary ( element )
7595
76- const result = await element . executeAsync ( executeQuery , [
96+ const result = await element . executeAsync < any [ ] , any [ ] > ( executeQuery , [
7797 queryName ,
7898 element ,
7999 ...serializeArgs ( args ) ,
@@ -95,17 +115,17 @@ function createQuery(element, queryName) {
95115 }
96116}
97117
98- function within ( element ) {
118+ function within ( element : Element ) {
99119 return Object . keys ( baseQueries ) . reduce (
100120 ( queries , queryName ) => ( {
101121 ...queries ,
102122 [ queryName ] : createQuery ( element , queryName ) ,
103123 } ) ,
104124 { } ,
105- )
125+ ) as WebdriverIOQueries
106126}
107127
108- async function setupBrowser ( browser ) {
128+ async function setupBrowser ( browser : BrowserObject | MultiRemoteBrowserObject ) {
109129 const body = await browser . $ ( 'body' )
110130 const queries = within ( body )
111131
@@ -117,8 +137,8 @@ async function setupBrowser(browser) {
117137 browser . addCommand (
118138 queryName ,
119139 function ( ...args ) {
120- // eslint-disable-next-line babel/no-invalid-this
121- return within ( this ) [ queryName ] ( ...args )
140+ // @ts -expect-error
141+ return within ( this as Element ) [ queryName ] ( ...args )
122142 } ,
123143 true ,
124144 )
@@ -127,12 +147,9 @@ async function setupBrowser(browser) {
127147 return queries
128148}
129149
130- function configure ( config ) {
150+ function configure ( config : Partial < Config > ) {
131151 _config = config
132152}
133153
134- module . exports = {
135- within,
136- setupBrowser,
137- configure,
138- }
154+ export * from './types'
155+ export { within , setupBrowser , configure }
0 commit comments