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

Cypress › Extra questions from common lists

How do you control the order in which specs run?

The order of spec files is not guaranteed, so tests must be independent, and specPattern and --spec only select files. Inside one file tests run top to bottom. Order dependence breaks under parallel runs, retries and re-running a single file. Shared setup goes into beforeEach, an API call or cy.session, not into a separate "test".

Cypress · Practice and details

The checkout test passes only after the login test. Is that a problem or normal?

A typical question: «How do you set the order in which specs run?». Behind it is another situation: checkout.cy.ts works only when login.cy.ts ran before it, because that one created a user or a session. While the run is sequential, everything is green. The first parallel run, retry or single-file run breaks such a suite. The lesson covers what you can and should not do about order.

How it works and why

Which files run is set by specPattern (a string or an array of globs in the config) and --spec in the CLI. The documentation describes them as file selection. It gives no contract about the order between files, so tests must not be built on it. Inside one file the order is reliable: tests run top to bottom in declaration order (this is how Mocha works).

Why depending on order hurts. First, testIsolation clears browser state between tests, so a «login from the previous test» will not survive anyway. Second, retries (retries) rerun only the failed test, without the earlier ones. Third, parallel runs in CI spread specs across machines in no fixed order. Fourth, rerunning a single broken test never passes if it depends on a neighbour.

Order dependency and independent tests

fragile

Test B needs test A

Breaks under parallelism, retries, a single run.

robust

Each test prepares its own data

The user or session is created through the API before the test.

Figure 1. Independent tests give both parallelism and isolated reruns.

Example: before → after

// before: checkout relies on login.cy.ts
// login.cy.ts
it('creates a user and logs in', () => { /* ... */ });

// checkout.cy.ts
it('places an order', () => {
  cy.visit('/checkout'); // will not work without login
});
// after: each test prepares what it needs
beforeEach(() => {
  cy.request('POST', '/api/test/users', { email: 'qa@example.com' });
  cy.session('qa', () => {
    cy.request('POST', '/api/login', { email: 'qa@example.com' });
  });
  cy.visit('/checkout');
});

To run only part of the suite use file selection: npx cypress run --spec "cypress/e2e/checkout/**". If a scenario really consists of steps that make no sense separately (a long wizard form), write one test with several steps and an explanation in its name, rather than several dependent ones.

A login shared across files comes from cy.session with the cacheAcrossSpecs: true option: the first file that needs the session creates it and the others reuse the cache, so no file depends on something having run earlier. Independence is checked by running each file alone, for example in a CI loop.

# each spec alone: one that depends on a neighbour will fail
fail=0
for f in cypress/e2e/*.cy.ts; do npx cypress run --spec "$f" || { echo "FAILED: $f"; fail=1; }; done
exit $fail

Limits and alternatives

  • Top-to-bottom order inside a file can serve readability but not as a dependency: it.only or a retry will break it.
  • Shared setup goes into beforeEach, cy.session or an API request, not into a separate «test».
  • In Playwright the principle is the same: tests are independent and workers spread files arbitrarily; for a deliberately sequential scenario there is test.describe.configure({ mode: 'serial' }), but it is an exception.

Common mistakes

  • Relying on spec order and not noticing the problem until parallel runs are enabled.
  • Passing data between tests through a variable or a file: in a retry or a single run it is not there.
  • Naming files 01-login.cy.ts, 02-order.cy.ts instead of removing the dependency: it only masks it.

Check yourself

The suite is green when sequential and red when parallel. What is the most likely cause and how do you check?

Answer

Tests depend on order or on shared server state. Check: run one «red» file alone through --spec; if it fails on its own, the dependency is confirmed.

Why does retries not save a dependent test?

Answer

A retry repeats only the failed test in a clean state (testIsolation) without the earlier ones. It does not restore data or a session from a neighbouring test.

In an interview. Junior: The files to run are chosen with specPattern and --spec, and inside a file tests go in declaration order. Middle/Senior: I do not build order between files into the logic: dependent tests break under parallel runs, retries and single reruns. I create data and the session through the API and cy.session in beforeEach.

More articles in this chapter