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

Cypress › Command queue and asynchrony

Why can't you write await cy.get() in Cypress?

Cypress commands do not run when called and are not Promises: they are put in a queue and run after the test body has finished. So await does nothing for them, and cy.get() returns not an element or a string but a chain object (Chainable). Ordinary JS runs immediately, before the commands, so values are taken with .then(), and a state that changes is checked with .should().

Cypress · Asynchrony

You write const text = await cy.get(...) like in Playwright and the test fails with a strange error. Why?

A developer used to Playwright or Jest brings the habit of putting await in front of every call. In Cypress it either does nothing or the test fails with a message that you returned a Promise and also invoked cy commands. This lesson explains why: what cy.get() actually does when it is called and when the command really runs.

How it works and why. According to the Cypress documentation, commands do not run when they are invoked. They are added to a queue and run after the test function has exited. The queue goes serially: each command waits until the previous one has finished together with its retries. Cypress commands are not Promises: you cannot await them, cannot race or parallelise them, and cannot attach a .catch. A failed command simply fails the test.

When cy.get() runs

1. The test body runscy.get() only adds a command to the queue2. The test body has endedall the plain JS has already run3. Cypress runs the queuecommands one by one, each waits for the last
Figure 1. The test body only builds the queue. Everything Cypress does with the browser happens after it.

This explains the "strange" order of execution. Plain JS (console.log, arithmetic, assignments) runs immediately while the test body is still executing, and Cypress commands wait for their turn:

it('execution order', () => {
  cy.visit('/cart');
  console.log('A');
  cy.get('h1').then(() => console.log('B'));
  console.log('C');
});
// in the console: A, C, B

In the same way, cy.get('h1').invoke('text') returns not a string but a chain object (a Chainable). The string appears only when Cypress reaches that command, and until then it does not exist.

Example: from await to .then and .should. A typical attempt "like in Playwright":

it('cart title', async () => {
  cy.visit('/cart');
  const text = await cy.get('h1').invoke('text');
  expect(text).to.contain('Cart');
});

An async function always returns a Promise, and Cypress does not allow returning a Promise from a test that also invoked cy commands: it shows an error about mixing the two. Even when such code sometimes "works", the execution order is not guaranteed. The correct version separates two jobs: getting a value and checking a state.

it('cart title', () => {
  cy.visit('/cart');
  // a value: in .then
  cy.get('h1').invoke('text').then((text) => {
    expect(text).to.contain('Cart');
  });
  // a check: in .should, which also retries
  cy.get('h1').should('contain', 'Cart');
});

The difference between them matters: in .then() the callback runs once, while .should('contain', ...) is retried until the check passes or the timeout ends. For a state that changes over time, the first form is fragile and the second is stable. More on this in the lesson about .then() and .should().

Limits and alternatives. The queue does not mean there is no asynchronous code in tests. A command result is taken through .then() or an alias, a Promise from your own code is wrapped in cy.wrap(), and what must run in Node (database, files) goes to cy.task(). Inside .then(async () => ...) you can await ordinary Promises, but not cy commands. If you really need async/await at every step, that is an argument for Playwright, not a reason to work around the queue.

And in Playwright:

The opposite: every action returns a real Promise and await is mandatory. The typical mistake there is a forgotten await: the action is not awaited and the test becomes flaky. The @typescript-eslint/no-floating-promises rule catches it.

const text = await page.locator('h1').textContent();
await expect(page.locator('h1')).toContainText('Cart');

Typical mistakes.

  • Writing await cy.get(...) and making the test async: this mixes a Promise with the command queue.
  • Assigning a command result to a variable (const el = cy.get(...)) and using it as an element or text: the variable holds only a Chainable.
  • Expecting plain JS after cy.get() to run after it: it runs earlier. Conditional logic that uses page values goes inside .then().
  • Adding cy.wait(1000) instead of fixing the order: the problem is not time but when the code runs.

Check yourself

A test has let total; cy.get('.total').invoke('text').then(t => { total = t }); console.log(total). What does the console print and why?

Answer

undefined. The test body runs completely before the queue starts, so console.log fires before .then assigns the value. Anything that depends on total must be inside .then() or in later queued commands.

Why can an async test with await cy.get() not be fixed by simply adding await before every command?

Answer

Cypress commands are not Promises: they are queued and the queue is run by Cypress itself after the test body. An async function returns a Promise, and Cypress forbids returning a Promise while also invoking cy commands. Take values with .then, check them with .should.

In the interview. Junior: Cypress commands do not return a Promise, they return a chainable object. Calling cy.get only puts a command into a queue, and Cypress itself runs the queue serially after the test body has finished. That is why await does not work here. I take values with then and check them with should, because should also retries. Middle/Senior: The queue gives Cypress control over waiting and retries, but the price is that plain JS and commands live in different "times". So I put anything that depends on a page value inside then, wrap a Promise from my own code in cy.wrap, and move Node code to cy.task. And if the project really needs async/await at every step, that is an argument for Playwright.

More articles in this chapter