Skip to content

Commit

Permalink
fix: add missing Cypress.Commands.addAll() types (#20894)
Browse files Browse the repository at this point in the history
Co-authored-by: Emily Rohrbough <[email protected]>
  • Loading branch information
sainthkh and emilyrohrbough committed Apr 11, 2022
1 parent c523b92 commit 4815a56
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
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

3 comments on commit 4815a56

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4815a56 Apr 11, 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.4/linux-x64/develop-4815a5644f5d33cdd7f5f6cec9be92712ca2b6dc/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4815a56 Apr 11, 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.4/darwin-x64/develop-4815a5644f5d33cdd7f5f6cec9be92712ca2b6dc/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4815a56 Apr 11, 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.4/win32-x64/develop-4815a5644f5d33cdd7f5f6cec9be92712ca2b6dc/cypress.tgz

Please sign in to comment.