Cypress · Browser testing end to end
How does the Cypress command chain work?
Short answer
Each cy command is enqueued rather than executed immediately. Cypress runs the queue in order, and most commands yield a subject to the next command, which is why chaining reads like a pipeline.
When a test calls cy.get, cy.click or cy.contains, the call does not perform the action right away. It adds an entry to a command queue. After the test function returns, Cypress drains the queue one command at a time, waiting for each command and its assertions before moving on.
it("runs commands in order", () => {
cy.visit("/products");
cy.get("[data-cy=search]").type("chair");
cy.get("[data-cy=result]")
.should("have.length", 3);
cy.log("the queue is complete");
});Subjects and yielding
Most commands yield a subject. cy.get yields the matched element, and the next command in the chain receives it. That is why cy.get then .click works: click operates on the element from get. Commands such as cy.visit and cy.wait yield their own subjects, so chaining depends on what the previous command produced.
- Commands are queued, not executed at the call site
- The queue runs serially, which makes tests deterministic
- The last command in a chain is the one that retries its assertions
- cy.wrap lets you put a plain value back into the chain
- Inside a .then callback you can run ordinary JavaScript on the subject
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why does cy.log run after the commands written before it?
- 2What does a command yield to the next command?
- 3How do you use a value from one command in a later one?
- 4Is the Cypress command chain a promise chain?
Say it in the interview
Cypress commands are queued instead of running immediately, and the runner drains the queue in order. Most commands yield a subject that the next command receives, which is what makes chaining work. The last command in a chain is the one whose assertions retry. When I need to use a value later, I use then, an alias or cy.wrap rather than trying to read it synchronously.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Cypress mock interview and get scored feedback on depth, clarity and confidence.