Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix warning "Cypress detected that you returned a promise in a test"

Tags:

cypress

I'm writing a test in cypress that checks that a new item was added to the list. I don't want to hardcode the length, I'd like to confirm that the size of the list increased by one. This code works:

  beforeEach(() => {
    cy.visit('/page/1');
  });

  it('happy path comments', async function() {
    cy.get('[data-cy=commentList]').should('have.length', 1);
    const list = await cy.get('[data-cy=commentList] [data-cy=comment]');
    const beforeLength = list.length;
    cy.get('[data-cy=commentBody]').type('foobar');
    cy.get('[data-cy=submit]').click();
    cy.get('.[data-cy=commentList] [data-cy=comment]').should(
      'have.length',
      beforeLength + 1
    );
    cy.get('[data-cy=commentBody]').should('have.value', '');
  });

However, this generates the following warning:

cypress_runner.js:84852 Cypress Warning: Cypress detected that you returned a promise in a test, but also invoked one or more cy commands inside of that promise.

The test title was:

Comments happy path comments

While this works in practice, it's often indicative of an anti-pattern. You almost never need to return both a promise and also invoke cy commands.

How can I fix this warning?

like image 494
AlexMA Avatar asked Jul 27 '19 02:07

AlexMA


People also ask

How do I stop error checking in Cypress?

If you'd like to force Cypress to interact with the element there are a few options: Pass {force: true} . This disables all error checking. Pass {waitForAnimations: false} to disable animation error checking.

How do I return a promise in Cypress?

Use Cypress. Promise to create promises. Cypress is promise aware so if you return a promise from inside of commands like . then() , Cypress will not continue until those promises resolve.

What is uncaught exception in Cypress?

To handle the error from the Web page under test, cypress provides the special command. cy. on('uncaught:exception', (err, runnable) => {}) This command always listens to the exceptions return false and will ignore these errors from failing tests.

Are Cypress commands promises?

Cypress commands are not complete Promises, as they can't run in parallel and can't have explicit exception handling with ".


1 Answers

Remove async/await from your test. Cypress commands are not promises, although behaving like promises. Read more here.

Also, you cannot assign or work with the return values of any Cypress command. Commands are enqueued and run asynchronously. What commands really return is Chainable, in other word, it's kind of queue object which resolves with the desired value. Read more here.

like image 52
Yevhen Laichenkov Avatar answered Oct 02 '22 22:10

Yevhen Laichenkov