I’m building a backend API test suite using Cypress and cy.request. I know cy.request returns a Cypress promise (Cypress.Chainable), so manually wrapping it in JavaScript promises shouldn’t be necessary. However, I need to make API calls sequentially without chaining .then().
The code runs successfully in Cypress open mode (Chrome), but when executing in headless mode, the second request consistently fails with the error: “Cypress test was stopped while running this command.” It appears Cypress isn’t resolving the custom-wrapped promise when running headless.
How can I handle await with Cypress promises reliably in headless environments?
I’ve been there too! So, here’s the thing, Cypress commands (like cy.request
) aren’t true async/await promises; they’re actually queued commands. It can be a bit confusing because it looks like you’re waiting for something, but internally, Cypress is managing its own command queue.
Now, in headless mode, Cypress is a little more rigid and doesn’t allow test flow to step outside of its command queue, which is why it can cause issues with async calls.
What worked for me was ditching await
altogether and chaining commands with .then()
. It might seem like a bit of a step back in terms of readability, but I found that if I wrapped my logic in functions and chained them, it kept everything Cypress-friendly while ensuring things ran smoothly.
Absolutely, I ran into the same problem while dealing with multiple API validations in one test. The issue often happens because Cypress doesn’t fully handle custom async/await wrappers well in headless mode, especially when the test exits before a Cypress command is resolved.
To solve it, I removed any custom async/await logic around cy.request
. Instead, I went for a more traditional chaining approach, like this:
Yeah, this is a pretty common problem, especially when running in CI or headless mode where timing is much more sensitive. Since Cypress doesn’t behave like native JavaScript here, it struggles to track async calls outside its internal command chain.
This can cause issues like tests finishing prematurely before all commands are executed. What helped me was entirely switching to using Cypress’s own commands without mixing in native await
.
I stuck with .then()
to control flow, even though it seemed redundant at first. If you want to keep things clean, you can also create custom commands to simplify this nesting logic. It really makes the test more reliable overall, especially in headless mode