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: types for Cypress.Commands.add (#20376) #20377

Merged
merged 10 commits into from
Apr 15, 2022
7 changes: 4 additions & 3 deletions cli/types/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ declare namespace Cypress {
type PrevSubject = keyof PrevSubjectMap
type TestingType = 'e2e' | 'component'
type PluginConfig = (on: PluginEvents, config: PluginConfigOptions) => void | ConfigOptions | Promise<ConfigOptions>

type JQuerySelector = JQuery & { selector: string }
atarek12 marked this conversation as resolved.
Show resolved Hide resolved
interface PrevSubjectMap<O = unknown> {
optional: O
element: JQuery
element: JQuerySelector
document: Document
window: Window
}
Expand Down Expand Up @@ -461,8 +461,9 @@ declare namespace Cypress {
Commands: {
add<T extends keyof Chainable>(name: T, fn: CommandFn<T>): void
add<T extends keyof Chainable>(name: T, options: CommandOptions & {prevSubject: false}, fn: CommandFn<T>): void
add<T extends keyof Chainable, S = any>(name: T, options: CommandOptions & {prevSubject: true}, fn: CommandFnWithSubject<T, S>): void
add<T extends keyof Chainable, S extends PrevSubject>(
name: T, options: CommandOptions & { prevSubject: true | S | ['optional'] }, fn: CommandFnWithSubject<T, PrevSubjectMap[S]>,
name: T, options: CommandOptions & { prevSubject: S | ['optional'] }, fn: CommandFnWithSubject<T, PrevSubjectMap[S]>,
): void
add<T extends keyof Chainable, S extends PrevSubject>(
name: T, options: CommandOptions & { prevSubject: S[] }, fn: CommandFnWithSubject<T, PrevSubjectMap<void>[S]>,
Expand Down
15 changes: 9 additions & 6 deletions cli/types/tests/cypress-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace CypressCommandsTests {
arg // $ExpectType string
})
Cypress.Commands.add('newCommand', { prevSubject: true }, (subject, arg) => {
subject // $ExpectType unknown
subject // $ExpectType any
arg // $ExpectType string
return
})
Expand Down Expand Up @@ -113,11 +113,13 @@ namespace CypressCommandsTests {
arg // $ExpectType string
})
Cypress.Commands.add('newCommand', { prevSubject: 'element' }, (subject, arg) => {
subject // $ExpectType JQuery<HTMLElement>
subject // $ExpectType JQuerySelector
subject.selector // $ExpectType string
arg // $ExpectType string
})
Cypress.Commands.add('newCommand', { prevSubject: ['element'] }, (subject, arg) => {
subject // $ExpectType JQuery<HTMLElement>
subject // $ExpectType JQuerySelector
subject.selector // $ExpectType string
arg // $ExpectType string
})
Cypress.Commands.add('newCommand', { prevSubject: ['element', 'document', 'window'] }, (subject, arg) => {
Expand All @@ -126,7 +128,8 @@ namespace CypressCommandsTests {
} else if (subject instanceof Document) {
subject // $ExpectType Document
} else {
subject // $ExpectType JQuery<HTMLElement>
subject // $ExpectType JQuerySelector
subject.selector // $ExpectType string
}
arg // $ExpectType string
})
Expand All @@ -136,7 +139,7 @@ namespace CypressCommandsTests {
} else if (subject instanceof Document) {
subject // $ExpectType Document
} else if (subject) {
subject // $ExpectType JQuery<HTMLElement>
subject // $ExpectType JQuerySelector
} else {
subject // $ExpectType void
}
Expand All @@ -159,7 +162,7 @@ namespace CypressCommandsTests {
originalFn.apply(this, [arg]) // $ExpectType Chainable<number>
})
Cypress.Commands.overwrite<'type', 'element'>('type', (originalFn, element, text, options?: Partial<Cypress.TypeOptions & {sensitive: boolean}>) => {
element // $ExpectType JQuery<HTMLElement>
element // $ExpectType JQuerySelector
text // $ExpectType string

if (options && options.sensitive) {
Expand Down