Skip to content
Advertisement

Is there a reliable way to have Cypress exit as soon as a test fails?

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):

// cypress/plugins/index.js

let shouldSkip = false;
module.exports = ( on ) => {
  on('task', {
    resetShouldSkipFlag () {
      shouldSkip = false;
      return null;
    },
    shouldSkip ( value ) {
      if ( value != null ) shouldSkip = value;
      return shouldSkip;
    }
  });
}
// cypress/support/index.js

function abortEarly () {
  if ( this.currentTest.state === 'failed' ) {
    return cy.task('shouldSkip', true);
  }
  cy.task('shouldSkip').then( value => {
    if ( value ) this.skip();
  });
}

beforeEach(abortEarly);
afterEach(abortEarly);

before(() => {
  if ( Cypress.browser.isHeaded ) {
    // Reset the shouldSkip flag at the start of a run, so that it 
    //  doesn't carry over into subsequent runs.
    // Do this only for headed runs because in headless runs,
    //  the `before` hook is executed for each spec file.
    cy.task('resetShouldSkipFlag');
  }
});

Will skip all further tests once a failure is encountered. The output will look like:

enter image description here

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement