From 4815a5644f5d33cdd7f5f6cec9be92712ca2b6dc Mon Sep 17 00:00:00 2001 From: Kukhyeon Heo Date: Tue, 12 Apr 2022 02:25:11 +0900 Subject: [PATCH] fix: add missing Cypress.Commands.addAll() types (#20894) Co-authored-by: Emily Rohrbough --- cli/types/cypress.d.ts | 14 ++++ cli/types/tests/cypress-tests.ts | 112 +++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) diff --git a/cli/types/cypress.d.ts b/cli/types/cypress.d.ts index 0cca2c56e5eb..bfb7c84822fd 100644 --- a/cli/types/cypress.d.ts +++ b/cli/types/cypress.d.ts @@ -24,9 +24,15 @@ declare namespace Cypress { interface CommandFn { (this: Mocha.Context, ...args: Parameters): ReturnType | void } + interface CommandFns { + [name: string]: (this: Mocha.Context, ...args: any) => any + } interface CommandFnWithSubject { (this: Mocha.Context, prevSubject: S, ...args: Parameters): ReturnType | void } + interface CommandFnsWithSubject { + [name: string]: (this: Mocha.Context, prevSubject: S, ...args: any) => any + } interface CommandOriginalFn extends CallableFunction { (...args: Parameters): ReturnType } @@ -467,6 +473,14 @@ declare namespace Cypress { add( name: T, options: CommandOptions & { prevSubject: S[] }, fn: CommandFnWithSubject[S]>, ): void + addAll(fns: CommandFns): void + addAll(options: CommandOptions & {prevSubject: false}, fns: CommandFns): void + addAll( + options: CommandOptions & { prevSubject: true | S | ['optional'] }, fns: CommandFnsWithSubject, + ): void + addAll( + options: CommandOptions & { prevSubject: S[] }, fns: CommandFnsWithSubject[S]>, + ): void overwrite(name: T, fn: CommandFnWithOriginalFn): void overwrite(name: T, fn: CommandFnWithOriginalFnAndSubject): void } diff --git a/cli/types/tests/cypress-tests.ts b/cli/types/tests/cypress-tests.ts index ca80e00d71a4..eecb36f65909 100644 --- a/cli/types/tests/cypress-tests.ts +++ b/cli/types/tests/cypress-tests.ts @@ -147,6 +147,118 @@ namespace CypressCommandsTests { arg return cy.wrap(new Promise((resolve) => { resolve(5) })) }) + + Cypress.Commands.addAll({ + newCommand(arg) { + // $ExpectType any + arg + this // $ExpectType Context + return + }, + newCommand2(arg, arg2) { + // $ExpectType any + arg + // $ExpectType any + arg2 + }, + newCommand3: (arg) => { + // $ExpectType any + arg + return + }, + newCommand4: (arg) => { + // $ExpectType any + arg + }, + }) + Cypress.Commands.addAll({ prevSubject: true }, { + newCommand: (subject, arg) => { + subject // $ExpectType unknown + arg // $ExpectType any + return + }, + }) + Cypress.Commands.addAll({ prevSubject: false }, { + newCommand: (arg) => { + arg // $ExpectType any + return + }, + }) + Cypress.Commands.addAll({ prevSubject: 'optional' }, { + newCommand: (subject, arg) => { + subject // $ExpectType unknown + arg // $ExpectType any + return + }, + newCommand2: (subject, arg) => { + subject // $ExpectType unknown + arg // $ExpectType any + }, + }) + Cypress.Commands.addAll({ prevSubject: ['optional'] }, { + newCommand: (subject, arg) => { + subject // $ExpectType unknown + arg // $ExpectType any + }, + }) + Cypress.Commands.addAll({ prevSubject: 'document' }, { + newCommand: (subject, arg) => { + subject // $ExpectType Document + arg // $ExpectType any + }, + }) + Cypress.Commands.addAll({ prevSubject: 'window' }, { + newCommand: (subject, arg) => { + subject // $ExpectType Window + arg // $ExpectType any + }, + }) + Cypress.Commands.addAll({ prevSubject: 'element' }, { + newCommand: (subject, arg) => { + subject // $ExpectType JQuery + arg // $ExpectType any + } + }) + Cypress.Commands.addAll({ prevSubject: ['element'] }, { + newCommand: (subject, arg) => { + subject // $ExpectType JQuery + arg // $ExpectType any + } + }) + Cypress.Commands.addAll({ prevSubject: ['element', 'document', 'window'] }, { + newCommand: (subject, arg) => { + if (subject instanceof Window) { + subject // $ExpectType Window + } else if (subject instanceof Document) { + subject // $ExpectType Document + } else { + subject // $ExpectType JQuery + } + arg // $ExpectType any + } + }) + Cypress.Commands.addAll({ prevSubject: ['window', 'document', 'optional', 'element'] }, { + newCommand: (subject, arg) => { + if (subject instanceof Window) { + subject // $ExpectType Window + } else if (subject instanceof Document) { + subject // $ExpectType Document + } else if (subject) { + subject // $ExpectType JQuery + } else { + subject // $ExpectType void + } + arg // $ExpectType any + } + }) + Cypress.Commands.addAll({ + newCommand: (arg) => { + // $ExpectType any + arg + return cy.wrap(new Promise((resolve) => { resolve(5) })) + } + }) + Cypress.Commands.overwrite('newCommand', (originalFn, arg) => { arg // $ExpectType string originalFn // $ExpectedType Chainable['newCommand']