Mastering Test Flow Control with Cypress.stop()

Mastering Test Flow Control with Cypress.stop()
Mastering Test Flow Control with Cypress.stop()

Cypress is a powerful tool for end-to-end testing, but sometimes you need to halt execution immediately when a critical failure occurs. Enter Cypress.stop(). This command stops all test execution, ensuring you avoid cascading failures or wasted resources when a prerequisite fails. Let’s explore how to use it effectively. When working with automated tests in Cypress, efficiency is everything—especially when you're running large test suites across multiple environments or CI pipelines. That’s where Cypress's Cypress.stop() method becomes incredibly useful.

What is Cypress.stop()?

Cypress.stop() halts the Cypress Test Runner mid-execution. It’s a manual switch to stop running any remaining tests in the current spec file.

It can be incredibly handy in situations like:

  • When an essential precondition fails (e.g., token generation, environment setup)
  • When you want to stop on the first failure
  • To prevent redundant test execution during debugging or early failure diagnosis

Syntax

Cypress.stop()

Example Scenario: Stop Test Run on Token Generation Failure

Let’s say you’re working on API tests.

In this API test, Cypress.stop() is used to halt execution if an access token can’t be generated. Without this token, other tests verifying are meaningless. Here’s the breakdown:

it('Generates an Access Token', () => {
  cy.request({
    method: 'POST',
    url: `${Cypress.env('baseUrl')}`,
    headers: {
      'subscription-Key': `${Cypress.env('subscriptionKey')}`,
      Authorization:
        'Basic ' +
        btoa(
          `${Cypress.env('apiUser')}:${Cypress.env('apiKey')}`
        ),
    },
    failOnStatusCode: false,
  }).then((response) => {
    if (response.status !== 200 || !response.body.access_token) {
      cy.log('Access token generation failed!');
      Cypress.stop(); //Stop remaining tests in the file
    }
    cy.log(‘Access token generated successfully');
    Cypress.env('accessToken', response.body.access_token);
  });
});
// ... other tests that depend on the access token

Stop Tests After Any Failure

If you want to automatically stop the test run on any failure, add this to your automation script:

afterEach(function () {
  if (this.currentTest.state === 'failed') {
    Cypress.stop();
  }
});

Behavior Differences: cypress run vs cypress open

  • cypress run (CI mode): Cypress.stop() halts execution of the current spec file, but the app continues running and uploads all test results.
  • cypress open (Interactive mode): Cypress will stop executing tests but keep the Test Runner open for inspection.

Why This Works

  • Fail-Fast Principle: The test suite stops immediately on token failure, avoiding false negatives in later steps.
  • Clear Diagnostics: Logs the failure and stops execution, making debugging easier.

When to Use Cypress.stop() vs. cy.skip()

Cypress.stop():

  • Nuclear option: Stops ALL tests.
  • Use when no further tests can proceed (e.g., missing tokens, broken environments).

cy.skip():

  • Skips a single test or block.
  • Use when a test is conditionally irrelevant (e.g., a feature flag is off).

Cypress.stop() is simple but powerful for controlling your test flow when you know something has gone wrong. It's especially useful during development or in CI pipelines to avoid wasting resources.

Cypress.stop() is your emergency brake for tests. By using it in scenarios like failed logins, broken API handshakes, or missing environment variables, you keep your test results clean and actionable. Pair it with robust error logging, and you’ll streamline your debugging process while saving time and resources.

Read more