cy.each() can not handle commands/assertion if they are inside a promise
See original GitHub issueCurrent 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:
- Created 2 years ago
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
What your examples are showing:
What I’m trying to do:
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.
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:
– 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