QA Interview Hub · Knowledge base · Quizzes · Українська

Cypress › Command queue and asynchrony

The asynchrony of Cypress commands

Cypress commands do not run when called but go into a queue that starts after the test body finishes. Hence three places: ordinary JS in the test body runs at once and builds the queue, commands and the .then and .should callbacks run in the queue, and Node code goes through cy.task. Most bugs with variables and Promises come from mixing these places.

Cypress · Asynchrony

A chain of commands looks like ordinary code but behaves differently. What short model explains it?

This lesson sums up the chapter. If you read a Cypress test as ordinary JS from top to bottom, it seems to break expectations: variables are empty, await does not work, an if on an element guesses wrong. Below is a model of three "places" that explains almost everything.

How it works and why. According to the documentation, Cypress commands do not run when they are called: they go into a queue that starts after the test body finishes, command by command. So there are three places: at once runs ordinary JS in the test body (which also builds the queue), in the queue run the commands and the .then and .should callbacks, and in the Node process runs what the browser cannot do, through cy.task. Every unusual symptom is a mix of these places.

Three places of execution

At onceplain JS in the test body, building the queueIn the queuecy commands, .then, .should: one by oneIn Nodecy.task, setupNodeEvents
Figure 1. Plain JS runs earlier than the commands: anything that depends on command results must stand in the queue.

Example: a condition on page state. Beginners write if (cy.get('banner').length), and it does not work: cy.get() returns a Chainable that has no such property, and when the body runs the element has not been looked up yet. The condition must stand in the queue:

cy.get('body').then(($body) => {
  // conditional logic on page state: inside .then
  if ($body.find('[data-cy=banner]').length) {
    cy.get('[data-cy=close-banner]').click();
  }
});

Here .then receives body, and $body.find() searches synchronously: it neither waits nor fails when there is no banner. cy.get('[data-cy=banner]').then(...) does not fit here: with no banner cy.get() fails after the timeout. For conditions on a state that may change, the Cypress documentation in general advises not to build tests on them, because they are unstable.

A quick choice of tool:

What you needWhat to use
Wait for a state to change.should(...)
Take a value once.then(cb)
A value in several places.as() and cy.get('@name')
Your own Promisecy.wrap(null).then(() => p())
Code in Nodecy.task()

Limits and alternatives. This model gives reliable retries and shorter tests, but it sometimes makes code with branching and loops unnatural: a loop over elements is better written with .each(), not for. If tests need real asynchronous logic at every step, that points to Playwright, where async/await replaces the queue.

And in Playwright:

There is no command queue: code runs top to bottom and await returns the result at once. A condition like if (await banner.count()) works the way it reads.

Typical mistakes.

  • Reading a command result in the test body, outside .then.
  • Putting an if on an element outside the queue.
  • Adding cy.wait(ms) instead of the right order and the right wait.
  • Mixing async/await with commands in one block.

Check yourself

Why does if (cy.get('.banner').length) not work?

Answer

Because cy.get() does not return elements: it puts a command in the queue and returns a Chainable, and the test body runs before the queue. A condition on page state goes inside .then().

Where does a plain console.log placed between two cy commands run?

Answer

Right away, while the test body is still building the queue, that is before both commands. To print something at the right point of the queue, use cy.log or console.log inside .then().

In the interview. Junior: Cypress commands do not run at once, they are queued and run serially after the test body. They return a chainable, not a Promise, so await does not work. I take values through then, and to keep them I use an alias. Middle/Senior: I have a simple model of three places: plain JS runs at once, commands and callbacks run in the queue, and Node code goes through cy.task. Most bugs with variables, ifs on elements and Promises are a mix of these places.

More articles in this chapter