Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add missing Cypress.Commands.addAll() types #20894

Merged
merged 6 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions cli/types/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ declare namespace Cypress {
interface CommandFn<T extends keyof ChainableMethods> {
(this: Mocha.Context, ...args: Parameters<ChainableMethods[T]>): ReturnType<ChainableMethods[T]> | void
}
interface CommandFns {
[name: string]: (this: Mocha.Context, ...args: any) => any
}
interface CommandFnWithSubject<T extends keyof ChainableMethods, S> {
(this: Mocha.Context, prevSubject: S, ...args: Parameters<ChainableMethods[T]>): ReturnType<ChainableMethods[T]> | void
}
interface CommandFnsWithSubject<S> {
[name: string]: (this: Mocha.Context, prevSubject: S, ...args: any) => any
}
interface CommandOriginalFn<T extends keyof ChainableMethods> extends CallableFunction {
(...args: Parameters<ChainableMethods[T]>): ReturnType<ChainableMethods[T]>
}
Expand Down Expand Up @@ -467,6 +473,14 @@ declare namespace Cypress {
add<T extends keyof Chainable, S extends PrevSubject>(
name: T, options: CommandOptions & { prevSubject: S[] }, fn: CommandFnWithSubject<T, PrevSubjectMap<void>[S]>,
): void
addAll<T extends keyof Chainable>(fns: CommandFns): void
addAll<T extends keyof Chainable>(options: CommandOptions & {prevSubject: false}, fns: CommandFns): void
addAll<T extends keyof Chainable, S extends PrevSubject>(
options: CommandOptions & { prevSubject: true | S | ['optional'] }, fns: CommandFnsWithSubject<PrevSubjectMap[S]>,
): void
addAll<T extends keyof Chainable, S extends PrevSubject>(
options: CommandOptions & { prevSubject: S[] }, fns: CommandFnsWithSubject<PrevSubjectMap<void>[S]>,
): void
overwrite<T extends keyof Chainable>(name: T, fn: CommandFnWithOriginalFn<T>): void
overwrite<T extends keyof Chainable, S extends PrevSubject>(name: T, fn: CommandFnWithOriginalFnAndSubject<T, PrevSubjectMap[S]>): void
}
Expand Down
112 changes: 112 additions & 0 deletions cli/types/tests/cypress-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,118 @@ namespace CypressCommandsTests {
arg
return cy.wrap(new Promise<number>((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<HTMLElement>
arg // $ExpectType any
}
})
Cypress.Commands.addAll({ prevSubject: ['element'] }, {
newCommand: (subject, arg) => {
subject // $ExpectType JQuery<HTMLElement>
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<HTMLElement>
}
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<HTMLElement>
} else {
subject // $ExpectType void
}
arg // $ExpectType any
}
})
Cypress.Commands.addAll({
newCommand: (arg) => {
// $ExpectType any
arg
return cy.wrap(new Promise<number>((resolve) => { resolve(5) }))
}
})

Cypress.Commands.overwrite('newCommand', (originalFn, arg) => {
arg // $ExpectType string
originalFn // $ExpectedType Chainable['newCommand']
Expand Down