Cypress.stop
Stop the Cypress App on the current machine while tests are running. This can be useful for stopping test execution upon failures or other predefined conditions.
Auto Cancellation: If you're looking to automatically stop all tests across multiple machines when a test fails, consider using the Auto Cancellation feature in Cypress Cloud.
Benefits: Stopping a test run on the first failure across parallelized machines will:
- Save time. Identify failures early and resolve issues faster.
- Reduce CI costs. Cut down on unnecessary test execution, leading to significant savings for large test suites.
- Free up CI resources. Prioritize critical tests and keep CI pipelines available for validating fixes.
- Optimize future runs. With Spec Prioritization and Auto Cancellation, failed tests run first in the next attempt, stopping early if issues persist to maximize efficiency.
Syntax​
Cypress.stop()
Examples​
Stop tests when a test fails​
To ensure tests stop immediately after a failure across any spec file, add the following snippet to your support/index.js
file:
afterEach(function () {
if (this.currentTest.state === 'failed') {
Cypress.stop()
}
})
Abort tests when a condition is met​
beforeEach(() => {
if (env !== 'expected-condition') {
cy.log('Stop tests - environment is not setup correctly')
Cypress.stop()
}
})
Notes​
cypress run
vs cypress open
behavior​
Calling Cypress.stop()
during cypress run
will skip any remaining tests in the current specfile. If recording to Cypress Cloud, all screenshots, videos, and Test Replay will still successfully upload.
Calling Cypress.stop()
during cypress open
will stop execution of the Cypress App, but remain open for inspection. The remaining tests will not run.
Why choose Auto Cancellation?​
Auto Cancellation is available with Cypress Cloud's Business+ plan. It offers several advantages over Cypress.stop
for stopping tests on failure:
- Scope of Cancellation:
Cypress.stop
halts only the current spec file, skipping remaining tests within it. Auto Cancellation, however, stops all tests across all machines and marks the entire run as cancelled in Cypress Cloud for better visibility. - Configurable Thresholds: Auto Cancellation allows you to define failure thresholds.
Cypress.stop
executes immediately when the specified condition is met. - Simplified Configuration: Auto Cancellation settings can be managed in Cypress Cloud, whereas
Cypress.stop
requires manual code changes. - Optimization with Spec Prioritization: Combined with Spec Prioritization (another Business+ feature), Auto Cancellation helps efficiently allocate resources by running previously failing specs first in a new run.
To get started with Cypress Cloud, sign up to start your 2 week free trial - including all premium Cypress Cloud features and plenty of test results to let you experience the power of Cypress Cloud!