Skip to content

Commit 4815a56

Browse files
fix: add missing Cypress.Commands.addAll() types (#20894)
Co-authored-by: Emily Rohrbough <emilyrohrbough@users.noreply.github.com>
1 parent c523b92 commit 4815a56

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

cli/types/cypress.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ declare namespace Cypress {
2424
interface CommandFn<T extends keyof ChainableMethods> {
2525
(this: Mocha.Context, ...args: Parameters<ChainableMethods[T]>): ReturnType<ChainableMethods[T]> | void
2626
}
27+
interface CommandFns {
28+
[name: string]: (this: Mocha.Context, ...args: any) => any
29+
}
2730
interface CommandFnWithSubject<T extends keyof ChainableMethods, S> {
2831
(this: Mocha.Context, prevSubject: S, ...args: Parameters<ChainableMethods[T]>): ReturnType<ChainableMethods[T]> | void
2932
}
33+
interface CommandFnsWithSubject<S> {
34+
[name: string]: (this: Mocha.Context, prevSubject: S, ...args: any) => any
35+
}
3036
interface CommandOriginalFn<T extends keyof ChainableMethods> extends CallableFunction {
3137
(...args: Parameters<ChainableMethods[T]>): ReturnType<ChainableMethods[T]>
3238
}
@@ -467,6 +473,14 @@ declare namespace Cypress {
467473
add<T extends keyof Chainable, S extends PrevSubject>(
468474
name: T, options: CommandOptions & { prevSubject: S[] }, fn: CommandFnWithSubject<T, PrevSubjectMap<void>[S]>,
469475
): void
476+
addAll<T extends keyof Chainable>(fns: CommandFns): void
477+
addAll<T extends keyof Chainable>(options: CommandOptions & {prevSubject: false}, fns: CommandFns): void
478+
addAll<T extends keyof Chainable, S extends PrevSubject>(
479+
options: CommandOptions & { prevSubject: true | S | ['optional'] }, fns: CommandFnsWithSubject<PrevSubjectMap[S]>,
480+
): void
481+
addAll<T extends keyof Chainable, S extends PrevSubject>(
482+
options: CommandOptions & { prevSubject: S[] }, fns: CommandFnsWithSubject<PrevSubjectMap<void>[S]>,
483+
): void
470484
overwrite<T extends keyof Chainable>(name: T, fn: CommandFnWithOriginalFn<T>): void
471485
overwrite<T extends keyof Chainable, S extends PrevSubject>(name: T, fn: CommandFnWithOriginalFnAndSubject<T, PrevSubjectMap[S]>): void
472486
}

cli/types/tests/cypress-tests.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,118 @@ namespace CypressCommandsTests {
147147
arg
148148
return cy.wrap(new Promise<number>((resolve) => { resolve(5) }))
149149
})
150+
151+
Cypress.Commands.addAll({
152+
newCommand(arg) {
153+
// $ExpectType any
154+
arg
155+
this // $ExpectType Context
156+
return
157+
},
158+
newCommand2(arg, arg2) {
159+
// $ExpectType any
160+
arg
161+
// $ExpectType any
162+
arg2
163+
},
164+
newCommand3: (arg) => {
165+
// $ExpectType any
166+
arg
167+
return
168+
},
169+
newCommand4: (arg) => {
170+
// $ExpectType any
171+
arg
172+
},
173+
})
174+
Cypress.Commands.addAll({ prevSubject: true }, {
175+
newCommand: (subject, arg) => {
176+
subject // $ExpectType unknown
177+
arg // $ExpectType any
178+
return
179+
},
180+
})
181+
Cypress.Commands.addAll({ prevSubject: false }, {
182+
newCommand: (arg) => {
183+
arg // $ExpectType any
184+
return
185+
},
186+
})
187+
Cypress.Commands.addAll({ prevSubject: 'optional' }, {
188+
newCommand: (subject, arg) => {
189+
subject // $ExpectType unknown
190+
arg // $ExpectType any
191+
return
192+
},
193+
newCommand2: (subject, arg) => {
194+
subject // $ExpectType unknown
195+
arg // $ExpectType any
196+
},
197+
})
198+
Cypress.Commands.addAll({ prevSubject: ['optional'] }, {
199+
newCommand: (subject, arg) => {
200+
subject // $ExpectType unknown
201+
arg // $ExpectType any
202+
},
203+
})
204+
Cypress.Commands.addAll({ prevSubject: 'document' }, {
205+
newCommand: (subject, arg) => {
206+
subject // $ExpectType Document
207+
arg // $ExpectType any
208+
},
209+
})
210+
Cypress.Commands.addAll({ prevSubject: 'window' }, {
211+
newCommand: (subject, arg) => {
212+
subject // $ExpectType Window
213+
arg // $ExpectType any
214+
},
215+
})
216+
Cypress.Commands.addAll({ prevSubject: 'element' }, {
217+
newCommand: (subject, arg) => {
218+
subject // $ExpectType JQuery<HTMLElement>
219+
arg // $ExpectType any
220+
}
221+
})
222+
Cypress.Commands.addAll({ prevSubject: ['element'] }, {
223+
newCommand: (subject, arg) => {
224+
subject // $ExpectType JQuery<HTMLElement>
225+
arg // $ExpectType any
226+
}
227+
})
228+
Cypress.Commands.addAll({ prevSubject: ['element', 'document', 'window'] }, {
229+
newCommand: (subject, arg) => {
230+
if (subject instanceof Window) {
231+
subject // $ExpectType Window
232+
} else if (subject instanceof Document) {
233+
subject // $ExpectType Document
234+
} else {
235+
subject // $ExpectType JQuery<HTMLElement>
236+
}
237+
arg // $ExpectType any
238+
}
239+
})
240+
Cypress.Commands.addAll({ prevSubject: ['window', 'document', 'optional', 'element'] }, {
241+
newCommand: (subject, arg) => {
242+
if (subject instanceof Window) {
243+
subject // $ExpectType Window
244+
} else if (subject instanceof Document) {
245+
subject // $ExpectType Document
246+
} else if (subject) {
247+
subject // $ExpectType JQuery<HTMLElement>
248+
} else {
249+
subject // $ExpectType void
250+
}
251+
arg // $ExpectType any
252+
}
253+
})
254+
Cypress.Commands.addAll({
255+
newCommand: (arg) => {
256+
// $ExpectType any
257+
arg
258+
return cy.wrap(new Promise<number>((resolve) => { resolve(5) }))
259+
}
260+
})
261+
150262
Cypress.Commands.overwrite('newCommand', (originalFn, arg) => {
151263
arg // $ExpectType string
152264
originalFn // $ExpectedType Chainable['newCommand']

0 commit comments

Comments
 (0)