Warm tip: This article is reproduced from serverfault.com, please click

javascript-一旦测试失败,是否有可靠的方法可以使赛普拉斯退出?

(javascript - Is there a reliable way to have Cypress exit as soon as a test fails?)

发布于 2019-11-01 10:46:33

我们在CI服务器上运行着一个大型测试套件,并且似乎无法告诉Cypress如果测试失败则退出。它始终运行整个套件。

有一些讨论,在这里,但没有可行的解决方案。

一旦测试失败,是否有可靠的方法可以使赛普拉斯退出?

Questioner
Undistraction
Viewed
11
dwelle 2019-11-19 19:23:08

正如你所提到的,它尚未正式受支持(从3.6.0版开始)。

这是我的一种破解方法(使用cookie和保持状态的方法):

// 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');
  }
});

一旦遇到故障,将跳过所有进一步的测试。输出将如下所示:

在此处输入图片说明