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.

cy.type('') should be treated as no-op instead of throwing an error

See original GitHub issue

Current behavior:

Calling cy.type with the empty string throws the error

CypressError: cy.type() cannot accept an empty String. You need to actually type something.

I saw https://github.com/cypress-io/cypress/issues/3587 that was closed, but the request there seemed to imply that passing the empty string to cy.type() should implicitly clear the input.

Instead, I propose that cy.type('') should simply be a no-op. The rationale is that it sometimes makes sense to include cy.type() in a chain of commands even when the input could be the empty string. In these situations, instead of being able to include the cy.type() even when “typing” the empty string would be a no-op, we have to special-case the empty string to prevent the error above.

For example, I would like the ability to write a helper function for testing many different inputs to a field with the following pattern:

testInput(inputValue) {
  cy.get(<inputField>)
    .clear()
    .type(inputValue);
}

But instead, I have to do the following, just to avoid passing the empty string to cy.type(). In this example, I want to run the rest of the commands in the chain, but I can’t chain cy.type() due to the current behavior:

testInput(inputValue) {
  cy.get(<inputField>)
    .clear()
    .then(e => { if (inputValue !== '') cy.wrap(e).type(inputValue) });
}

Desired behavior:

Calling cy.type('') should be a no-op, and return the same subject it was called on. It should behave as if the customer literally typed nothing.

Test code to reproduce

describe("type", () => {
  testSearch(inputValue) {
    cy.get('input')
      .clear()
      .type(inputValue)
      .type('{Enter}');
  }

  before(() => {
    cy.visit('https://www.google.com');
  });

  it('Searches for documentation on cy.type', () => {
    testSearch('cy.type');
  });

  it('Tries an empty search', () => {
    testSearch(''); // Currently explodes
});

Versions

Cypress 3.8.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
CodeTrooperscommented, May 7, 2020

I have the same need. It would be nice if it could be activated from options

0reactions
matthiasmoiercommented, Oct 21, 2020

We have the same need in the test setup for our app and used your proposed workaround. But personally, I would also prefer to have this as an option in Cypress directly rather than overwriting the command.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error Messages
We found an error preparing your test file. This message means that Cypress encountered an error when compiling and/or bundling your test file....
Read more >
Cypress does not always executes click on element
I am automating Google Calculator. And from time to time Cypress is not able to execute click on button. The tests click on...
Read more >
Converse.js API Documentation Source: headless/dist/ ...
'number') { throw new Error('Bad segment "' + segment + '", must be 0-based ... Try to return a Trusted Type object instead...
Read more >
Developer Guide: Migrating from Previous Versions
cancel() will throw an error if called with a promise that was not generated by $interval() . Previously, it would silently do nothing....
Read more >
cypress-io/cypress - Gitter
expect(() => { cy.request('BAD'); }).to.throw(new Error('PPP')); I get the following: AssertionError: expected [Function] to throw an error. I would expect ...
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