-
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
Cypress.Commands.overwrite
typescript problem appearing in 9.1
#19095
Comments
@lgenzelis Have you taken a look at #18915 or #18940 at all? This appears to be a duplicate issue of these. |
hey @emilyrohrbough , neither of those issues even mention |
Hey @lgenzelis -- sorry for the jump that this was a duplicate. The issues I linked both have PRs open to add additional types to Cypress.Commands to resolve type issues being seen for Cypress.Commands.add & Cypress.Commands.overrite. |
Hey @lgenzelis ! My PR #19003 should fix the issue from "Test code to reproduce" example. Regarding the overwriting commands in loopI'm not sure whether it is even possible currently in TS to provide typings that will support this case. I think it's ok to use some workarounds in your code. For example, this will work when/if my PR got merged: if (Cypress.env('demoMode') === 'on') {
// adapted from https://github.com/cypress-io/cypress/issues/249#issuecomment-670028947
for (const commandName of ['visit', 'click', 'trigger', 'type', 'clear', 'reload'] as const) {
// we add 2s delays for a few commands so that stakeholders can see what's going on
const commandWithDelay = ((command: (...args: unknown[]) => unknown, ...args: unknown[]) =>
new Promise((resolve) => {
setTimeout(() => resolve(command(...args)), 2000);
})) as any as Cypress.CommandFnWithOriginalFn<typeof commandName>;
Cypress.Commands.overwrite(commandName, commandWithDelay);
}
} |
Yep, it looks like the type for |
Thanks @xumepadismal ! That would be a big improvement over the current state. So, what you're saying is that you don't think it's possible to add types to the case where we return a promise in the overwritten command, right? Let's stick to the easier case from the docs. So, this would work (just copy-pasting the same example again): Cypress.Commands.overwrite('type', (originalFn, element, text, options) => {
if (options && options.sensitive) {
// turn off original log
options.log = false
// create our own log with masked message
Cypress.log({
$el: element,
name: 'type',
message: '*'.repeat(text.length),
})
}
return originalFn(element, text, options)
}) but this wouldn't: Cypress.Commands.overwrite('type', async (originalFn, element, text, options) => {
if (options && options.sensitive) {
// turn off original log
options.log = false
// create our own log with masked message
Cypress.log({
$el: element,
name: 'type',
message: '*'.repeat(text.length),
})
}
return originalFn(element, text, options)
}) (notice the I don't know about the intricacies of cypress typing, but if |
@lgenzelis I meant the for-of loop problem, not the Promises itself. TS not very good with narrowing types for the iterable. Regarding the async functions or returning a Promise I had some trouble with typing those cases, too. The approach like Blah | Promise works bad here because you'll end up TS requiring you to return Chainable | Promise<Chainable> which is kind of absurd. I'm not saying this is impossible to fix but will likely require to adjust typings in places outside the custom commands, too. It feels like out of scope of my PR now. AFAIK docs officially don't explicitly support this so I think we should go with a separate follow-up PR to address this and update the docs. For now you can use |
Hey @xumepadismal , I'm looking for a way to try your PR to check it out, but I'm being unsuccessful 😞 Mind giving me a hand when you've got a minute please? Things I've tried:
Cypress.Commands.overwrite('type', (originalFn, element, text, options) => {
if (options?.sensitive) {
// turn off original log
options.log = false;
// create our own log with masked message
Cypress.log({
$el: element,
name: 'type',
message: '*'.repeat(text.length),
});
}
return originalFn(element, text, options);
}); checking that raises the error
Maybe I should also copy some other files from your branch? |
The code for this is done in cypress-io/cypress#19003, but has yet to be released. |
Hey @lgenzelis Please refer to this new paragraph in docs It is from my PR cypress-io/cypress-documentation#4263 to Cypress Docs repo |
Awesome! I can't wait to see it released! 😄 Thanks @xumepadismal |
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
Current behavior
Cypress 9 added type definitions to
Cypress.Commands.overwrite
, and I can't seem to make sense of them.This is what I have, in
cypress/support/commands.ts
:This was working fine until the new Cypress version.
Let's tackle something simpler, taken out straight from cypress docs:
I can't get the types right, even for that seemingly simple example 😞
Desired behavior
No response
Test code to reproduce
Cypress Version
9.1
Other
No response
The text was updated successfully, but these errors were encountered: