question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Cypress.Commands.overwrite` typescript problem appearing in 9.1

See original GitHub issue

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:

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);
      });

    Cypress.Commands.overwrite(commandName, commandWithDelay);
  }
}

This was working fine until the new Cypress version.

Let’s tackle something simpler, taken out straight from cypress docs:

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)
})

I can’t get the types right, even for that seemingly simple example 😞

Desired behavior

No response

Test code to reproduce

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)
})

Cypress Version

9.1

Other

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
xumepadismalcommented, Nov 30, 2021

Hey @lgenzelis ! My PR #19003 should fix the issue from “Test code to reproduce” example.

Regarding the overwriting commands in loop

I’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);
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript - Cypress Documentation
Cypress ships with official type declarations for TypeScript. ... When overwriting either built-in or custom commands which make use of prevSubject ...
Read more >
Cypress overwrite type command and extend .d.ts file with ...
you can retype the original command: /** * Type into a DOM element. * * @see https://on.cypress.io/type * @example ...
Read more >
cypress-io/cypress - Gitter
An error has occurred, please login again through your application. ... the problem - Cypress itself now prevents you from overwriting query commands....
Read more >
cypress - Awesome JS
Added the missing Cypress.Command.addAll() Typescript types. Fixed #18886. Fixed an uncommon error observed in cy.session() where an error was thrown
Read more >
Window: beforeunload event - Web APIs | MDN
The beforeunload event suffers from the same problems as the unload event ... Starting with Firefox 44, Chrome 51, Opera 38, and Safari...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found