Skip to content

Commit 798a1ff

Browse files
docs: Document Cypress.runner.stop
1 parent 8a81200 commit 798a1ff

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

docs/api/cypress-api/runner-api.mdx

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: 'Cypress.runner | Cypress Documentation'
3+
description: Stop Cypress on failure or other conditions
4+
sidebar_label: runner
5+
---
6+
7+
<ProductHeading product="app" />
8+
9+
# Cypress.runner
10+
11+
The `Cypress.runner` API allows you to stop the Cypress App 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+
<Icon name="check-circle" color="green" /> **Benefits:** Canceling an **entire**
18+
test run upon the first test failure across parallelized machines will:
19+
20+
1. **Save time**. Resolve test outcomes faster.
21+
2. **Reduce CI costs**. These cost savings can be significant for large test
22+
suites.
23+
3. **Free-up CI resources** for validating fixes, and helping other users.
24+
25+
:::
26+
27+
## Syntax
28+
29+
```javascript
30+
Cypress.runner.stop
31+
```
32+
33+
## Examples
34+
35+
### Stop tests when a test fails
36+
37+
To ensure tests stop immediately after a failure across any spec file, add the following snippet to your `support/index.js` file:
38+
39+
```javascript
40+
afterEach(function() {
41+
if (this.currentTest.state === 'failed') {
42+
Cypress.runner.stop()
43+
}
44+
})
45+
```
46+
47+
### Abort tests when a condition is met
48+
49+
```javascript
50+
beforeEach(() => {
51+
if (env !== 'expected-condition') {
52+
cy.log('Stop tests - environment is not setup correctly')
53+
Cypress.runner.stop()
54+
}
55+
})
56+
```
57+
58+
## Notes
59+
60+
### Why Choose Auto Cancellation?
61+
62+
[Auto Cancellation](/cloud/features/smart-orchestration/run-cancellation) is available with Cypress Cloud's Business+ plan. It offers several advantages over `Cypress.runner.stop` for stopping tests on **failure**:
63+
64+
1. **Scope of Cancellation:** `Cypress.runner.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.
65+
2. **Configurable Thresholds:** Auto Cancellation allows you to define failure thresholds. `Cypress.runner.stop` executes immediately when the specified condition is met.
66+
3. **Simplified Configuration**: Auto Cancellation settings can be managed in Cypress Cloud, whereas `Cypress.runner.stop` requires manual code changes.
67+
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.
68+
69+
<CloudFreePlan />
70+
71+
## See also
72+
73+
- [Auto Cancellation](/cloud/features/smart-orchestration/run-cancellation)
74+
- [`Cypress.currentTest`](/api/cypress-api/currenttest)
75+
- [`Cypress.currentRetry`](/api/cypress-api/currentretry)
76+
- [Load Balancing](/cloud/features/smart-orchestration/load-balancing)
77+
- [Parallelization](/cloud/features/smart-orchestration/parallelization)
78+
- [Spec Prioritization](/cloud/features/smart-orchestration/spec-prioritization)

0 commit comments

Comments
 (0)