Skip to content

Commit ce33265

Browse files
authored
fix(types): Fix overwriteCommand inconsistent typing (#14549)
* Fix incorrect inference when overriding commands for element and browser - Fix inference having the `this` which was in excess - Fix inference wrapping in a `(...arg) =>` which was in excess - Fix browser command overwrite where the return promise type is wrong because undefined verbose return type is inferred to `Promise<unknown>` * Fix the debug return type * Fix typings * Fix UTs * Have more verbose and type safe examples * fix the type reference of the `this` argument + add UTs * Even better example * More type fixes + more robust UTs * Simplification of code in index.ts * Add this infer type for browser overwritten command + final review * Remove proposition to revisite later + fix typing issue
1 parent 2e40754 commit ce33265

29 files changed

Lines changed: 297 additions & 142 deletions

packages/webdriverio/src/commands/browser/custom$.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export async function custom$ (
3434
this: WebdriverIO.Browser,
3535
strategyName: string,
3636
...strategyArguments: unknown[]
37-
) {
37+
) : Promise<WebdriverIO.Element> {
3838
const strategy = this.strategies.get(strategyName) as CustomStrategyFunction
3939

4040
if (!strategy) {

packages/webdriverio/src/commands/browser/debug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import WDIORepl from '@wdio/repl'
3434
export function debug(
3535
this: WebdriverIO.Browser,
3636
commandTimeout = 5000
37-
) {
37+
): Promise<void | unknown> {
3838
const repl = new WDIORepl()
3939
const { introMessage } = WDIORepl
4040

packages/webdriverio/src/commands/browser/getWindowSize.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ interface BrowserSize {
2323
* @type window
2424
*
2525
*/
26-
export async function getWindowSize(this: WebdriverIO.Browser) {
26+
export async function getWindowSize(this: WebdriverIO.Browser): Promise<BrowserSize> {
2727
const browser = getBrowserObject(this)
2828

2929
const { width, height } = await browser.getWindowRect() as BrowserSize
30-
return { width, height } as BrowserSize
30+
return { width, height } satisfies BrowserSize
3131
}

packages/webdriverio/src/commands/browser/keys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { checkUnicode } from '../../utils/index.js'
2626
export async function keys (
2727
this: WebdriverIO.Browser,
2828
value: string | string[]
29-
) {
29+
): Promise<void> {
3030
let keySequence: string[] = []
3131

3232
/**

packages/webdriverio/src/commands/browser/mockClearAll.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const log = logger('webdriverio:mockClearAll')
3030
*
3131
* @alias browser.mockClearAll
3232
*/
33-
export async function mockClearAll () {
33+
export async function mockClearAll (): Promise<void> {
3434
for (const [handle, mocks] of Object.entries(SESSION_MOCKS)) {
3535
log.trace(`Clearing mocks for ${handle}`)
3636
for (const mock of mocks) {

packages/webdriverio/src/commands/browser/mockRestoreAll.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const log = logger('webdriverio:mockRestoreAll')
2828
*
2929
* @alias browser.mockRestoreAll
3030
*/
31-
export async function mockRestoreAll () {
31+
export async function mockRestoreAll (): Promise<void> {
3232
for (const [handle, mocks] of Object.entries(SESSION_MOCKS)) {
3333
log.trace(`Clearing mocks for ${handle}`)
3434
for (const mock of mocks) {

packages/webdriverio/src/commands/browser/pause.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
export function pause (
2323
this: WebdriverIO.Browser,
2424
milliseconds = 1000
25-
) {
25+
): Promise<void> {
2626
return new Promise((resolve) => setTimeout(resolve, milliseconds))
2727
}

packages/webdriverio/src/commands/browser/react$$.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export async function react$$ (
4343
this: WebdriverIO.Browser,
4444
selector: string,
4545
{ props = {}, state = {} }: ReactSelectorOptions = {}
46-
) {
46+
): Promise<WebdriverIO.ElementArray> {
4747
await this.executeScript(resqScript, [])
4848
await this.execute(waitToLoadReact)
4949
const res = await this.execute(

packages/webdriverio/src/commands/browser/react$.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export async function react$ (
5252
this: WebdriverIO.Browser,
5353
selector: string,
5454
{ props = {}, state = {} }: ReactSelectorOptions = {}
55-
) {
55+
): Promise<WebdriverIO.Element> {
5656
await this.executeScript(resqScript.toString(), [])
5757
await this.execute(waitToLoadReact)
5858
const res = await this.execute(

packages/webdriverio/src/commands/browser/reloadSession.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const log = logger('webdriverio')
5555
* @type utility
5656
*
5757
*/
58-
export async function reloadSession (this: WebdriverIO.Browser, newCapabilities?: WebdriverIO.Capabilities) {
58+
export async function reloadSession (this: WebdriverIO.Browser, newCapabilities?: WebdriverIO.Capabilities): Promise<string> {
5959
const oldSessionId = (this as WebdriverIO.Browser).sessionId
6060

6161
/**
@@ -91,5 +91,5 @@ export async function reloadSession (this: WebdriverIO.Browser, newCapabilities?
9191
await Promise.all(options.onReload.map((hook) => hook(oldSessionId, (this as WebdriverIO.Browser).sessionId)))
9292
}
9393

94-
return this.sessionId as string
94+
return this.sessionId
9595
}

0 commit comments

Comments
 (0)