Raj
Raj

Reputation: 29

Terminate / Skip / Stop all tests from all spec files if any one test fails in cypress

am trying to skip all other tests from all spec files if one test fails and found a working solution over here Is there a reliable way to have Cypress exit as soon as a test fails?. However, this looks to be working only if the test fails in it() assertions. How can we skip the tests if somethings fails in beforeach()

For eg:

    before(() => {

      cy.get('[data-name="email-input"]').type(email);
      cy.get('[data-name="password-input"]').type(email);
      cy.get('[data-name="account-save-btn"]').click();
});

And if something goes wrong (for eg: CypressError: Timed out retrying: Expected to find element: '[data-name="email-input"]', but never found it.) in above code then stop/ skip all tests in all spec files.

Upvotes: 0

Views: 3330

Answers (2)

Eva-Anna Klugman
Eva-Anna Klugman

Reputation: 336

As of Cypress 10, tests don't run if before or beforeEach hook fails.

Upvotes: 0

Raj
Raj

Reputation: 29

Just in case anyone is looking answer for the same question. I have found a solution and would like to share.

To implement the solution I have used a cookie that I will set to value true if something fails and before executing each test cypress will check the value of cookie. If the value of cookie is true it skips the test.

Cypress.on('fail', error => {
  document.cookie = "shouldSkip=true" ;
  throw error;
});

function stopTests() {
  cy.getCookie('shouldSkip').then(cookie => {
    if (cookie && typeof cookie === 'object' && cookie.value === 'true') {
      Cypress.runner.stop();
    }
  });
}

beforeEach(stopTests);

Also to note: Tests should be written in it() block and avoid using before() to write tests

Upvotes: 1

Related Questions