We have a large test suite running on a CI server, and there appears to be no way of telling Cypress to exit if a test fails. It always runs the entire suite.
There is some discussion here, but no workable solution.
Is there a reliable way to have Cypress exit as soon as a test fails?
Advertisement
Answer
As you’ve mentioned, it’s not officially supported yet (as of 3.6.0).
Here’s my take at a hack (without the use of cookies and such for keeping state):
JavaScript
x
16
16
1
// cypress/plugins/index.js
2
3
let shouldSkip = false;
4
module.exports = ( on ) => {
5
on('task', {
6
resetShouldSkipFlag () {
7
shouldSkip = false;
8
return null;
9
},
10
shouldSkip ( value ) {
11
if ( value != null ) shouldSkip = value;
12
return shouldSkip;
13
}
14
});
15
}
16
JavaScript
1
24
24
1
// cypress/support/index.js
2
3
function abortEarly () {
4
if ( this.currentTest.state === 'failed' ) {
5
return cy.task('shouldSkip', true);
6
}
7
cy.task('shouldSkip').then( value => {
8
if ( value ) this.skip();
9
});
10
}
11
12
beforeEach(abortEarly);
13
afterEach(abortEarly);
14
15
before(() => {
16
if ( Cypress.browser.isHeaded ) {
17
// Reset the shouldSkip flag at the start of a run, so that it
18
// doesn't carry over into subsequent runs.
19
// Do this only for headed runs because in headless runs,
20
// the `before` hook is executed for each spec file.
21
cy.task('resetShouldSkipFlag');
22
}
23
});
24
Will skip all further tests once a failure is encountered. The output will look like: