Skip to content

Commit

Permalink
fix: types for Cypress.Commands.add (cypress-io#20376)
Browse files Browse the repository at this point in the history
  • Loading branch information
atarek12 committed Feb 27, 2022
1 parent ffd3627 commit f6e6edd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
7 changes: 4 additions & 3 deletions cli/types/cypress.d.ts
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 }
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>(name: T, options: CommandOptions & {prevSubject: true}, fn: CommandFnWithSubject<T, PrevSubjectMap['element']>): 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
16 changes: 10 additions & 6 deletions cli/types/tests/cypress-tests.ts
Expand Up @@ -83,7 +83,8 @@ namespace CypressCommandsTests {
arg // $ExpectType string
})
Cypress.Commands.add('newCommand', { prevSubject: true }, (subject, arg) => {
subject // $ExpectType unknown
subject // $ExpectType JQuerySelector
subject.selector // $ExpectType string
arg // $ExpectType string
return
})
Expand Down Expand Up @@ -113,11 +114,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 +129,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 +140,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 +163,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

0 comments on commit f6e6edd

Please sign in to comment.