|
| 1 | +--- |
| 2 | +title: 'Cypress.stop() | Cypress Documentation' |
| 3 | +description: Stop Cypress on failure or any other conditions |
| 4 | +sidebar_label: stop |
| 5 | +--- |
| 6 | + |
| 7 | +<ProductHeading product="app" /> |
| 8 | + |
| 9 | +# Cypress.stop |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +:::tip |
| 14 | + |
| 15 | +**Auto Cancellation**: If you're looking to automatically stop _all tests_ across _multiple machines_ when a test fails, consider using the [Auto Cancellation feature](/cloud/features/smart-orchestration/run-cancellation) in Cypress Cloud. |
| 16 | + |
| 17 | +<AutoCancellationBenefits /> |
| 18 | + |
| 19 | +::: |
| 20 | + |
| 21 | +## Syntax |
| 22 | + |
| 23 | +```javascript |
| 24 | +Cypress.stop() |
| 25 | +``` |
| 26 | + |
| 27 | +## Examples |
| 28 | + |
| 29 | +### Stop tests when a test fails |
| 30 | + |
| 31 | +To ensure tests stop immediately after a failure across any spec file, add the following snippet to your `support/index.js` file: |
| 32 | + |
| 33 | +```javascript |
| 34 | +afterEach(function () { |
| 35 | + if (this.currentTest.state === 'failed') { |
| 36 | + Cypress.stop() |
| 37 | + } |
| 38 | +}) |
| 39 | +``` |
| 40 | + |
| 41 | +### Abort tests when a condition is met |
| 42 | + |
| 43 | +```javascript |
| 44 | +beforeEach(() => { |
| 45 | + if (env !== 'expected-condition') { |
| 46 | + cy.log('Stop tests - environment is not setup correctly') |
| 47 | + Cypress.stop() |
| 48 | + } |
| 49 | +}) |
| 50 | +``` |
| 51 | + |
| 52 | +## Notes |
| 53 | + |
| 54 | +### `cypress run` vs `cypress open` behavior |
| 55 | + |
| 56 | +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](/cloud/features/test-replay) will still successfully upload. |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +Calling `Cypress.stop()` during `cypress open` will stop execution of the Cypress App, but remain open for inspection. The remaining tests will not run. |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +### Why choose Auto Cancellation? |
| 65 | + |
| 66 | +[Auto Cancellation](/cloud/features/smart-orchestration/run-cancellation) is available with Cypress Cloud's Business+ plan. It offers several advantages over `Cypress.stop` for stopping tests on **failure**: |
| 67 | + |
| 68 | +1. **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. |
| 69 | +2. **Configurable Thresholds:** Auto Cancellation allows you to define failure thresholds. `Cypress.stop` executes immediately when the specified condition is met. |
| 70 | +3. **Simplified Configuration**: Auto Cancellation settings can be managed in Cypress Cloud, whereas `Cypress.stop` requires manual code changes. |
| 71 | +4. **Optimization with Spec Prioritization**: Combined with [Spec Prioritization](/cloud/features/smart-orchestration/spec-prioritization) (another Business+ feature), Auto Cancellation helps efficiently allocate resources by running previously failing specs first in a new run. |
| 72 | + |
| 73 | +<CloudFreePlan /> |
| 74 | + |
| 75 | +## See also |
| 76 | + |
| 77 | +- [Auto Cancellation](/cloud/features/smart-orchestration/run-cancellation) |
| 78 | +- [`Cypress.currentTest`](/api/cypress-api/currenttest) |
| 79 | +- [`Cypress.currentRetry`](/api/cypress-api/currentretry) |
| 80 | +- [Load Balancing](/cloud/features/smart-orchestration/load-balancing) |
| 81 | +- [Parallelization](/cloud/features/smart-orchestration/parallelization) |
| 82 | +- [Spec Prioritization](/cloud/features/smart-orchestration/spec-prioritization) |
0 commit comments