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

Cypress › CI, parallelization, reports

How to start the app before Cypress tests in CI?

The app is started in the background and the pipeline waits until its URL responds, since "the process is started" does not mean "the server listens". Without the wait the first cy.visit() fails with a connection error. The wait comes from wait-on, start-server-and-test or the start and wait-on fields of cypress-io/github-action, with a timeout. A health endpoint is a better target than the home page.

Cypress · CI

The server is started, yet the first test fails with a connection error. Why does "started" not mean "ready"?

Cypress tests open a live app, so in CI it has to be started before cypress run and given time to become ready. The difference between "the process is started" and "the server answers" looks small until the first cy.visit() fails on a slow machine. The lesson is about how to close that gap.

How it works and why. The command npm start & starts the process in the background and returns control at once. Before it listens on the port, the server still loads code, builds a bundle or connects to the database, and that takes from seconds to a minute. Cypress checks baseUrl at the start, and the first cy.visit() makes a request, so if the port is still closed, the test fails with a connection error. There is one cure: between start and tests put a step that blocks until the URL answers and has a timeout.

Start or wait

Wrong

npm start & cypress run

The background process only starts. Tests begin before the server listens on the port and the first visit fails with a connection error.

Right

start + wait-on

The waiting step blocks until the URL answers. Tests start only on a ready server, and a timeout stops a hang.

Figure 1. The difference is not whether the server is started but whether anyone checked that it answers.
  • wait-on blocks until the URL answers: start the server in the background, then npx wait-on http://localhost:3000, then Cypress.
  • start-server-and-test does it all in one call: it starts the server, waits for the URL, runs the test command and stops the server.
  • cypress-io/github-action has the fields start (the server command in the background), wait-on (the URL), wait-on-timeout (how many seconds to poll, 60 by default) and build for a build step.

Example: from a race to waiting. For any CI system a pair of commands or one call works:

npm start &
npx wait-on http://localhost:3000
npx cypress run

# or in one call
npx start-server-and-test start http://localhost:3000 "cypress run"

In GitHub Actions the action does the same. The build is moved to a separate step and the timeout is raised, because the first answer of a production build on a cold runner may take longer than the standard 60 seconds:

- uses: cypress-io/github-action@v7
  with:
    build: npm run build
    start: npm start
    wait-on: 'http://localhost:3000'
    wait-on-timeout: 120

The start field is not meant for launching Cypress itself: it only brings up the server, and the process ends with the job. The address of the environment under test is baseUrl: it can be changed for one run without editing the file:

# environment address for one run, without editing the file
npx cypress run --config baseUrl=https://staging.example.org
# the same as an environment variable
CYPRESS_BASE_URL=https://staging.example.org npx cypress run

Limits and alternatives. Waiting for a URL proves the server answers but not that its dependencies are ready: the port may open before the database. If the app has a separate health endpoint, wait for it rather than for the home page. Another trap (with older Node and wait-on versions): localhost may resolve to the IPv6 address ::1 while the server listens only on 127.0.0.1, and then the wait never finishes. In that case give wait-on the explicit address 127.0.0.1 instead of localhost. For tests on a production build (build and start) the result is closer to reality than on a dev server, which builds code on the fly and answers the first requests more slowly. If the app is already deployed (staging), no server needs to be started: baseUrl is enough. In Playwright this role is played by webServer in the config with the command and url fields, so the waiting is built into the tool itself.

Typical mistakes.

  • npm start & npx cypress run with no waiting: the first tests fail, a re-run passes.
  • Raising defaultCommandTimeout instead of adding a wait for the server: a command timeout does not cure a closed port.
  • Not limiting the wait by a timeout: a hung server holds the job until the global limit.
  • Putting the Cypress command itself into start: the field is meant only for the server.

Check yourself

Cypress in CI fails on the first cy.visit with a connection error although npm start is in the logs. What do you check and fix?

Answer

Whether there is a readiness wait between starting the server and the tests. If not, I add wait-on or start-server-and-test (in Actions the start and wait-on fields) with a timeout, so tests run only after the URL answers.

wait-on http://localhost:3000 hangs until the timeout although the server runs and opens in a browser at 127.0.0.1. What could it be?

Answer

localhost resolves to IPv6 (::1) while the server listens only on IPv4. Give wait-on the explicit address 127.0.0.1 or make the server listen on both.

In the interview. Junior: You cannot just start the server in the background and run the tests at once: you have to wait until the URL answers. wait-on and start-server-and-test exist for that, and in GitHub Actions cypress-io/github-action has the start and wait-on fields. I pass the environment address through baseUrl. Middle/Senior: I wait for a health endpoint rather than the home page, set a timeout, run a production build instead of a dev server and remember the IPv6 behaviour of localhost. For staging I start no server and only change baseUrl.

More articles in this chapter