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.each() can not handle commands/assertion if they are inside a promise

See original GitHub issue

Current behavior

So, I’ve read here that in order for .each to wait until previous loop is done, I need to return a promise inside the each callback. This works fine initially and even when I try to use async code like setTimeout and resolve inside it everything works well, just like docs describe.

However, calling setTimeout and resolving promise inside of it is hardly useful. The moment I try to call other cypress commands inside the promise (e.g. cy.get), cypress errors out.

Desired behavior

Cypress commands/assertions should works inside promises. Not being able to use them undermines the usability of cy.each greatly.

Test code to reproduce

it('Test fail', () => {
  cy.wrap([1, 2, 3]).each(x => {
    return new Cypress.Promise(resolve => {
      // Do cypress-y stuff here.
      cy.log(x)
      cy.wrap().then(resolve)
    })
  })
})

Cypress Version

9.1.0

Other

Theoretically, I should be able to rewrite everything inside the promise to use jquery and chai, but I’d like to stick to cypress syntax when possible.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rafaelrenecommented, Nov 29, 2021

What your examples are showing:

cy.wrap([1,,2,3]).each(x => {
  // cy commands here work just fine. This is shown in one of your examples.
  cy.log(x) // <--- this works
})

What I’m trying to do:

cy.wrap([1,2,3]).each(x => {
  return new Cypress.Promise(resolve => {
    // cy commands inside a promise do not work.
    //
    // if use-case is to call a promise and use cy commands on it's result in every loop this does not work.
    cy.log(x) // <--- causes test to fail
    cy.wrap().then(resolve)
  })
})

Now I will reiterate that this is not a big issue for me specifically, because I’ve already implemented what I needed to do without promises, but I’m reporting it because the docs seem to imply that it should work just fine and it doesn’t. Rather, nowhere is it mentioned that cy commands won’t work inside the promise.

I’m not sure if cypress team consider this a bug, if it’s hard to implement or if it’s even worth fixing as I do believe most people won’t ever run into this issue. Even I only ran into this issue after quite a long time of trying to figure out why my tests passed in headed mode, but constantly failed when headless. But if it is considered a bug, it should be fixed. If not, I believe that docs should be updated and mention that this does not work.

0reactions
bahmutovcommented, Nov 29, 2021

I now do not understand what the problem is. Here is an example of using Cy commands inside .each callback that sounds like what you are doing.

https://glebbahmutov.com/cypress-examples/9.0.0/commands/connectors.html#put-complex-logic-into-each-callback

and a few more examples are at https://glebbahmutov.com/cypress-examples/9.0.0/recipes/each-example.html

How is what you are doing different? Can you provide a test example with code?

Gleb

On Sun, Nov 28, 2021 at 7:31 PM René Rafael @.***> wrote:

@romankhomitskyi https://github.com/romankhomitskyi I don’t have trouble finding a workaround. I’m just reporting what I’d consider a bug. It should either be mentioned in docs that using commands inside a promise won’t work or it should just work.

Right now the docs make it seem as if it should work, but it doesn’t, so I made the issue.

Thanks for taking time out of your day to try and help.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/cypress-io/cypress/issues/19131#issuecomment-981192956, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQ4BJVK5NNM4HCAZOMXITTUOLCXXANCNFSM5I5S4RKQ .

– Dr. Gleb Bahmutov, PhD

Schedule video chat / phone call / meeting with me via https://calendly.com/bahmutov @.*** @bahmutov @.***> https://glebbahmutov.com/ https://glebbahmutov.com/blog https://github.com/bahmutov

Read more comments on GitHub >

github_iconTop Results From Across the Web

each - Cypress Documentation
Iterate through an array like structure (arrays or objects with a length property). It is unsafe to chain further commands that rely on...
Read more >
Fix The Dreaded Cypress Error "Command Inside Of A Promise"
In this video, I explain the dreaded "Cypress detected that you returned a promise in a test, but also invoked one or more...
Read more >
Stop cy.each Iteration When Using Cypress Commands Inside ...
If the cy. each callback uses Cypress commands and we want to stop the iteration based on the output of those commands, we...
Read more >
Cypress detected a promise when no promises were created
I think there's an error in declaring row => should be cy.get('row').last() . I don't believe that Cypress has finished yielded the element ......
Read more >
What are Cypress Assertions and How to use ... - Tools QA
Otherwise, it will not be feasible to validate whether the application reached the ... Similarly, what is an Explicit assertion in Cypress?
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