Skip to content

Commit

Permalink
fix: type 'this' on custom commands
Browse files Browse the repository at this point in the history
  • Loading branch information
KuznetsovRoman committed Mar 26, 2024
1 parent 29d1bc8 commit 3d131b8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/browser/commands/moveCursorTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type MoveToOptions = {
};

// As of now, we can't export type of the command function directly. See: https://github.com/webdriverio/webdriverio/issues/12527
export type MoveCursorToCommand = (options: MoveToOptions) => Promise<void>;
export type MoveCursorToCommand = (this: WebdriverIO.Element, options: MoveToOptions) => Promise<void>;

const makeMoveCursorToCommand = (session: WebdriverIO.Browser) =>
async function (this: WebdriverIO.Element, { xOffset = 0, yOffset = 0 }: MoveToOptions = {}): Promise<void> {
Expand Down
1 change: 1 addition & 0 deletions src/browser/commands/openAndWait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const is: Record<string, (match: Matches) => boolean> = {

const makeOpenAndWaitCommand = (config: BrowserConfig, session: WebdriverIO.Browser) =>
function openAndWait(
this: WebdriverIO.Browser,
uri: string,
{
selector = [],
Expand Down
13 changes: 11 additions & 2 deletions src/browser/commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,14 @@ export interface AssertViewOpts extends Partial<AssertViewOptsConfig> {
ignoreDiffPixelCount?: `${number}%` | number;
}

export type AssertViewCommand = (state: string, selectors: string | string[], opts?: AssertViewOpts) => Promise<void>;
export type AssertViewElementCommand = (state: string, opts?: AssertViewOpts) => Promise<void>;
export type AssertViewCommand = (
this: WebdriverIO.Browser,
state: string,
selectors: string | string[],
opts?: AssertViewOpts,
) => Promise<void>;
export type AssertViewElementCommand = (
this: WebdriverIO.Element,
state: string,
opts?: AssertViewOpts,
) => Promise<void>;
16 changes: 8 additions & 8 deletions src/browser/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace WebdriverIO {
interface Browser {
getMeta(): Promise<BrowserMeta>;
getMeta(key: string): Promise<unknown>;
getMeta(this: WebdriverIO.Browser): Promise<BrowserMeta>;
getMeta(this: WebdriverIO.Browser, key: string): Promise<unknown>;

setMeta(key: string, value: unknown): Promise<void>;
setMeta(this: WebdriverIO.Browser, key: string, value: unknown): Promise<void>;

extendOptions(opts: { [name: string]: unknown }): Promise<void>;
extendOptions(this: WebdriverIO.Browser, opts: { [name: string]: unknown }): Promise<void>;

getConfig(): Promise<BrowserConfig>;
getConfig(this: WebdriverIO.Browser): Promise<BrowserConfig>;

overwriteCommand<CommandName extends FunctionProperties<WebdriverIO.Browser>>(
name: CommandName,
Expand Down Expand Up @@ -104,7 +104,7 @@ declare global {
* @param stepCb step callback
* @returns {Promise<T>} value, returned by `stepCb`
*/
runStep<T = unknown>(stepName: string, stepCb: () => Promise<T> | T): Promise<T>;
runStep<T = unknown>(this: WebdriverIO.Browser, stepName: string, stepCb: () => Promise<T> | T): Promise<T>;

// TODO: describe executionContext more precisely
executionContext: (RunnerTest | RunnerHook) & {
Expand All @@ -119,9 +119,9 @@ declare global {

openAndWait: OpenAndWaitCommand;

switchToRepl: (ctx?: Record<string, unknown>) => Promise<void>;
switchToRepl: (this: WebdriverIO.Browser, ctx?: Record<string, unknown>) => Promise<void>;

clearSession: () => Promise<void>;
clearSession: (this: WebdriverIO.Browser) => Promise<void>;
}

interface Element {
Expand Down

0 comments on commit 3d131b8

Please sign in to comment.