We’re thrilled to announce Cypress.stop(), a powerful command that allows you to stop test execution programmatically.
Why we built Cypress.stop()
Testing should be efficient, predictable, and provide feedback as fast as possible. Whether you want to stop tests immediately upon failure or skip execution based on some other condition, Cypress.stop() provides faster feedback and the flexibility you need.
How Cypress.stop() works
With a single command, Cypress.stop() halts execution within the current spec file on the current machine. Here are some examples:
Stop tests on failure
To prevent further execution when a test fails, add the following snippet to your support/index.js file:
afterEach(function () {
if (this.currentTest.state === 'failed') {
Cypress.stop()
}
})
Stop tests based on other conditions
You may want to halt testing if something isn’t set up correctly in your test suite:
beforeEach(() => {
if (Cypress.env('TEST_ENV') !== 'expected-value') {
cy.log('Stopping tests due to incorrect environment')
Cypress.stop()
}
})
Should you use Cypress.stop() or Auto Cancellation?
If you’re looking to stop all tests across multiple machines when a failure occurs, consider using Auto Cancellation in Cypress Cloud. Auto Cancellation offers:
- ✅ Global scope: Stops all tests across all machines, whereas Cypress.stop() only halts the current spec file.
- ✅ Failure thresholds: Set custom failure limits before cancelling a run.
- ✅ Better resource allocation: Works with Spec Prioritization to optimize test execution.
- ✅ Centralized configuration: No need to modify your test files, failure thresholds can be updated directly in Cypress Cloud.
If you need complete run-level control, Auto Cancellation is the better choice and is available with a free trial of Cypress Cloud. However, for in-spec control and debugging, Cypress.stop() may fit your needs.
Try Cypress.stop() today!
This long-awaited feature is available now. Check out the official documentation for more details.
