在其中一个测试出错的情况下仅中断当前测试用例的方法

Way to break only current test case in case of error in one of tests

我使用 mocha 来测试使用 puppeteer 的网站。我有每个测试用例都有多个测试。

这里的问题是,如果任何测试失败,运行 进一步测试就没有意义了。

describe('testset 1', function() {
  let browser
  let page

  before(async () => {
    browser = new Browser() //
    page = await browser.newPage()
    await page.goto('/testset1')
  })

  it('test first step', () => {
    // this action opens modal required for step 2
    await page.click('.first-step-button')
    // for some reason modal is not opened and test is failed
    expect(true).to.equal(false)
  })

  it('test second step', () => {
    // No sense to do this because this selector is inside modal
    page.click('.first-step-button')
  })
})


describe('testset 2', function() {
  let browser
  let page

  before(async () => {
    browser = new Browser() //
    page = await browser.newPage()
    await page.goto('/testset2')
  })

  it('test first step', () => {
    // this action opens modal required for step 2
    await page.click('.first-step-button')
    // for some reason modal is not opened and test is failed
    expect(true).to.equal(false)
  })

  it('test second step', () => {
    // No sense to do this because this selector is inside modal
    page.click('.first-step-button')
  })
})

我想在第一次测试出错后从 testset 1 停止 运行ning 测试并切换到 testset 2

有没有办法只打断当前测试集,以防里面测试出错?

我试过 --bail mocha 选项,但它在第一个错误后立即停止测试,这是不希望的。即使我在 describe section

中这样做也是一样的行为
describe('testset 1', function() {
  this.bail(true)
})

我的解决方法

afterEach(function() {
  // this is pointed to current Mocha Context
  const {currentTest} = this
  if (currentTest.state === 'failed') {
    // parent is pointed to test set which contains current test
    currentTest.parent.pending = true
  }
})

现在一切正常

afterEach(function() {
  const {currentTest} = this
  if (currentTest.state === 'failed') {
    currentTest.parent.pending = true
  }
})

describe('testset 1', function() {
  it('test first step', () => {
    expect(true).to.equal(false)
  })

  it('test second step', () => {
    expect(true).to.equal(true)
  })
})

// this will be run after error in `testset 1`
describe('testset 2', function() {
  it('test first step', () => {
    // ...
  })

  // ...
})