Cypress: Uncaught Cypress: Return Promise Issue

Created on 30 May 2019  Â·  6Comments  Â·  Source: cypress-io/cypress

Current behavior:

I am facing very weird behavior in cypress... I have written a custom command in commands.js file, and using that command in spec file in different it function. The command passes in 1st 2 functions but fails in the third saying Uncaught CypressError: Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.. Although I have never used Promise.

Desired behavior:

The function should behave consistently same.

Steps to reproduce: (app code and test code)

In Spec file:

it('[1]FunctionName', function() {
    ……
    ……
    cy.filterByItemName(itemName);
}); 

it('[2]FunctionName', function() {
    cy.filterByItemName(itemName);
    ……  
    ……
    });

it('[3]FunctionName', function() {
    cy.filterByItemName(itemName);
    ……  
    ……
});

In commands.js file:

Cypress.Commands.add('filterByItemName', itemName => {
    cy.get('.cs-tag-list > .cs-tag-list__input').type('{backspace}' + itemName + '{enter}');
});

Versions

Cypress: 3.3.1
Mac Os

image

needs information

Most helpful comment

@jennifer-shehane Actually not. I found the real problem with this issue. It fires sometimes when the element is hidden underneath the other element. After many refactor of my own code and keeping up cypress best practices I still had that error.

So the main reason for this is Cypress just throwing this error message in different situations.

To repair this you need to implement more specific errors and find the place where try catch is used to 'globally'

All 6 comments

@jennifer-shehane , @brian-mann Can anyone please look into this issue, Since its a blocker for us.

Note: for more info, we have almost 20 it functions within spec file, and test fails in multiple places with the same error message. Even though we have never used Promise.

I'm unable to reproduce the error as described.

Please provide the exact code to run to reproduce this problem - @priyankaghosh1989 we cannot do any work on this without a reproducible example. It likely has to do with some other commands that are running around these functions.

The code I run:

commands.js

Cypress.Commands.add('filterByItemName', (itemName) => {
  cy.get('#email1').type(`{backspace}${itemName}{enter}`)
})

`my_spec.js

context('Promises', () => {
  beforeEach(() => {
    cy.visit('http://example.cypress.io/commands/actions')
  })

  it('[1]FunctionName', function () {
    cy.filterByItemName('foo')
  })

  it('[2]FunctionName', function () {
    cy.filterByItemName('bar')
  })

  it('[3]FunctionName', function () {
    cy.filterByItemName('baz')
  })
})

Hello @jennifer-shehane @brian-mann

To provide my exact code, I moved everything on .spec file (and removed command.js file). Please refer to the attachment. I provided the test-e2e.spec.js file along with the video. This error is blocking our entire automation. 😞

In the below example, I created 7 tests and test#5 has been failing for the same issue.

✓ [01]CreateNewPage (38722ms)
✓ [02]MoveToNewPage (23027ms)
✓ [03]MoveToExistingPage (20078ms)
✓ [04]SearchPage (14344ms)
1) [05]ExcludeFromPage
✓ [06]IncludeInExistingPage (26445ms)
✓ [07]DeletePage (22995ms)

This error is very weird,

  1. There is no return in the entire code.
  2. I use the same selector/code in every test, it passes for 1st few times then in one test it fails again it passes in the next tests.
    cy.get('.cs-tag-list__input').type(`{backspace}${mappingName}{enter}`);
  3. This error is not only with a specific cypress command. it appears with cy.get().type(), cy.get().click() etc.
  4. Sometimes it even fails in simple click() [Ex: cy.get('.return-link').click()]
  5. My observation is when you run a large number of tests/selectors it fails randomly with this error message.
Uncaught CypressError: Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.

The command that returned the promise was:

  > cy.get()

The cy command you invoked inside the promise was:

  > cy.get()

Because Cypress commands are already promise-like, you don't need to wrap them or return your own promise.

Cypress will resolve your command with whatever the final Cypress command yields.

The reason this is an error instead of a warning is because Cypress internally queues commands serially whereas Promises execute as soon as they are invoked. Attempting to reconcile this would prevent Cypress from ever resolving.

https://on.cypress.io/returning-promise-and-commands-in-another-command

This error originated from your test code, not from Cypress.

When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.

Screenshot 2019-06-09 at 11 59 33

Cypress Return Issue.zip

Hey @priyankaghosh1989, I'm pretty sure the suspect code is the code below:

...
cy.get('button.action-btn')
setTimeout(() => {
  cy.get('.csLoaderExplainer').should('not.exist');
}, 60000);
cy.get('.page-item')
...

This is being run completely outside of Cypress's commands, so this command will be queued at 60 seconds within your test run - which, at that point - who knows what Cypress command is currently running?

I'd suggest reading through some of our core concepts that explain how the commands run here: https://on.cypress.io/introduction-to-cypress#Commands-Are-Asynchronous

Could you explain exactly what you are trying to achieve with the code above in the setTimeout() within the Cypress tests? There is likely a better way to achieve the same results - and prevent the Promise error you are getting.

@jennifer-shehane Actually not. I found the real problem with this issue. It fires sometimes when the element is hidden underneath the other element. After many refactor of my own code and keeping up cypress best practices I still had that error.

So the main reason for this is Cypress just throwing this error message in different situations.

To repair this you need to implement more specific errors and find the place where try catch is used to 'globally'

i'm also getting this issue, haven't found anyone who's had this issue who's found a solution for it yet :/

Was this page helpful?
0 / 5 - 0 ratings

Related issues

weskor picture weskor  Â·  3Comments

zbigniewkalinowski picture zbigniewkalinowski  Â·  3Comments

stormherz picture stormherz  Â·  3Comments

rbung picture rbung  Â·  3Comments

simonhaenisch picture simonhaenisch  Â·  3Comments