Skip to content

Commit

Permalink
fix: types for Cypress.Commands.add (#20376) (#20377)
Browse files Browse the repository at this point in the history
Co-authored-by: Emily Rohrbough <emilyrohrbough@users.noreply.github.com>
  • Loading branch information
atarek12 and emilyrohrbough committed Apr 15, 2022
1 parent 71a18e4 commit 92d9425
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
9 changes: 6 additions & 3 deletions cli/types/cypress.d.ts
Expand Up @@ -10,10 +10,12 @@ declare namespace Cypress {
type PrevSubject = keyof PrevSubjectMap
type TestingType = 'e2e' | 'component'
type PluginConfig = (on: PluginEvents, config: PluginConfigOptions) => void | ConfigOptions | Promise<ConfigOptions>

interface JQueryWithSelector<TElement = HTMLElement> extends JQuery<TElement> {
selector?: string | null
}
interface PrevSubjectMap<O = unknown> {
optional: O
element: JQuery
element: JQueryWithSelector
document: Document
window: Window
}
Expand Down Expand Up @@ -467,8 +469,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
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 JQueryWithSelector<HTMLElement>
subject.selector // $ExpectType string | null | undefined
arg // $ExpectType string
})
Cypress.Commands.add('newCommand', { prevSubject: ['element'] }, (subject, arg) => {
subject // $ExpectType JQuery<HTMLElement>
subject // $ExpectType JQueryWithSelector<HTMLElement>
subject.selector // $ExpectType string | null | undefined
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 JQueryWithSelector<HTMLElement>
subject.selector // $ExpectType string | null | undefined
}
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 JQueryWithSelector<HTMLElement>
} else {
subject // $ExpectType void
}
Expand Down Expand Up @@ -271,7 +274,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 JQueryWithSelector<HTMLElement>
text // $ExpectType string

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

3 comments on commit 92d9425

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 92d9425 Apr 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/9.5.5/linux-x64/develop-92d94252ca90c10d65b4d8ce4a5b6bbebed87b18/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 92d9425 Apr 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/9.5.5/darwin-x64/develop-92d94252ca90c10d65b4d8ce4a5b6bbebed87b18/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 92d9425 Apr 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/9.5.5/win32-x64/develop-92d94252ca90c10d65b4d8ce4a5b6bbebed87b18/cypress.tgz

Please sign in to comment.