-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Conversation
Thanks for taking the time to open a PR!
|
cli/types/cypress.d.ts
Outdated
@@ -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>(args: CommandFnArgs<T>): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addAll<T extends keyof Chainable>(args: CommandFnArgs<T>): void | |
addAll<T extends keyof Chainable>(fn: CommandFnArgs<T>): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I named it args
because it's an object with functions. Or should we name it fns
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After leaving the comment above, I felt that fns
better describes the purpose of the argument than args
. I changed it that way.
cli/types/cypress.d.ts
Outdated
@@ -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 CommandFnArgs<T extends keyof ChainableMethods> { | |||
[name: string]: (this: Mocha.Context, ...args: any) => (ReturnType<ChainableMethods[T]> | void) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why ...args: any
instead of ...args: Parameters<ChainableMethods[T]>
? Could we re-using the existing CommandFn interface?
interface CommandFnArgs<T extends keyof ChainableMethods> {
[name: string]: CommandFn<T>
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first, I wrote it this way, because ...args: Parameters<ChainableMethods[T]>
doesn't work. TypeScript tries to interpret args
as the union type of all possible cy
method arg types (i.e. [type of cy.get parameters] | [type of cy.visit parameters] | etc... ).
This happened because <T extends keyof ChainableMethods>
part defined after the interface
means that this CommandFnArgs
interface should use the parameters of one of ChainableMethods
(i.e. one of the name of cy
methods) for all of its functions. It's not the right behavior.
So, I thought about using generics inside [key]
brackets. I realized that the purpose of addAll
is to add a new command. We're not reusing any cy
command. They should be any
, because we don't know what they are. They're defined by users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gottchya. That makes sense! Thank you for the explanation!
* 10.0-release: (25 commits) fix: stop running spec when switching specs (#21038) fix: remove asset size warnings and enable nuxt e2e tests (#21074) feat: swap the #__cy_root id selector to become data-cy-root for component mounting (#20951) fix: Doc changes around vue2 (#21066) feat: Add vue2 package from npm/vue/v2 branch (#21026) skip failing test fix: add possible frameworks to object API config (#21056) fix snapshot spacing fix system test fix snapshot update snapshots rename spec files rename files update snapshots release 9.5.4 [skip ci] fix(regression): cy.pause() should not be ignored with `cypress run --headed --no-exit` (#20877) fix: add missing Cypress.Commands.addAll() types (#20894) chore: Don't store video and screenshot artifacts for runs (#20979) chore: Update Chrome (beta) to 101.0.4951.26 (#20957) chore: remove parallelism from test-binary-against-repo jobs (#21004) ...
User facing changelog
Document
Cypress.Commands.addAll()
.Additional details
Cypress.Commands.addAll()
for those who want to add commands at once.any
because they can be anything. Otherwise, it breaks the type check.How has the user experience changed?
They can use
Cypress.Commands.addAll
.PR Tasks
cypress-documentation
? => docs: Document Cypress.Commands.addAll() cypress-documentation#4414type definitions
?cypress.schema.json
?