A tester who has just learned await moves to a Cypress project and writes what looks natural:
it('shows the number of loans', () => {
cy.visit('/loans');
const count = cy.get('[data-testid="loan-count"]').text();
expect(count).to.equal('3');
});
This fails before it reaches the page. cy.get(...) does not return an element, so there is no .text() to call.
The last five lectures taught one model: a call returns a promise, and await waits for it. Cypress uses a different model, and mixing the two is the main source of confusion for people who know both tools.
A command is an entry in a queue
When your test function runs, a Cypress command does not touch the browser. It adds an entry to a list, called the command queue, and returns at once. Cypress runs the entries in order after your test function has returned.
it('shows the number of loans', () => {
cy.visit('/loans'); // added to the queue
cy.get('[data-testid="loan-count"]'); // added to the queue
console.log('the test function is done'); // runs first
});
The console.log line runs before the page is visited. By the time any command runs, every line of the test function has already finished.
This explains the two things that do not work.
const count = cy.get(...)stores the object Cypress uses to build its chain of commands. It is not the element and not the text. The element does not exist yet when this line runs.await cy.get(...)does not do whatawaitdoes with a promise. A Cypress chain has athenmethod, but it is not a promise. The Cypress documentation says commands are not promises and cannot be awaited.
Use the value where it exists
The value is only available later, when the command runs. So you give Cypress the code that needs it. There are two ways.
Chain an assertion. Cypress retries it until it passes or the time runs out:
it('shows the number of loans', () => {
cy.visit('/loans');
cy.get('[data-testid="loan-count"]').should('have.text', '3');
});
Or use .then with a callback. Cypress calls it with the result of the command before it:
it('shows one row for each loan', () => {
cy.visit('/loans');
cy.get('[data-testid="loan-count"]').then(($count) => {
const expected = Number($count.text());
cy.get('[data-testid="loan-row"]').should('have.length', expected);
});
});
Inside the callback, $count is the real element, and $count.text() is a string you can use in ordinary JavaScript. The commands you add inside the callback are put in the queue right after the then.
This .then looks like the promise method and behaves differently. It is a Cypress command. You cannot attach .catch to it, and the Cypress documentation says a command chain is not a promise.
The addresses and test ids in these examples belong to the invented Shelf site. /loans is a relative path, and Cypress puts the baseUrl from its configuration in front of it.
What carries over from the promise lectures
The problem is the same one. A value from the page is not ready when your line runs. The two tools answer it differently.
- In Playwright and Selenium, the call returns a promise and you write
await. Your function pauses. - In Cypress, the call is queued and you pass a callback. Your function never pauses.
The mistakes carry over too. A plain variable assigned inside a .then callback and read outside it is the Cypress form of "a value read before the work finished". The line outside runs first.
When you join a Cypress project, read the tests for const x = cy. and for await cy.. Teams often lose time to a test that was written this way and passes only because its assertion never ran.
Remember this
A Cypress command adds an entry to a queue and returns at once. The value from the page exists only when the command runs later. Use the value in a chained assertion or inside .then, and do not use await on commands.
Reference: Introduction to Cypress, in the Cypress documentation.