繁体   English   中英

每次之后的Mocha更改超时

[英]Mocha change timeout for afterEach

我正在用mocha和chai编写一个节点应用程序。 一些测试调用用于集成测试的外部API,这可能最多需要90秒才能执行操作。

为了正确进行清理,我定义了afterEach() - afterEach() ,如果期望失败并且某些资源没有被删除,它将删除所有生成的远程资源。

测试本身会增加超时,而其余测试应保留其默认值和较小的超时:

it('should create remote resource', () => {...}).timeout(120000)

但是,我无法对afterEach().timeout(120000) ,因为该函数不存在-由于资源名称未知,我也不能使用function ()表示法:

describe('Resources', function () {
  beforeEach(() => {
    this._resources = null
  })

  it('should create resources', async () => {
    this._resources = await createResources()
    expect(false).to.equal(true)   // fail test, so...
    await deleteResources()        // will never be called
  })

  afterEach(async() => {
    this._resources.map(entry => {
      await // ... delete any created resources ...
    })
  }).timeout(120000)
})

有什么提示吗? Mocha是4.0.1版,chai是4.1.2版

所有Mocha区块的规则都相同。

可以使用以下命令在Mocha 1.x中为箭头功能设置timeout

  afterEach((done) => {
    // ...
    done();
  }).timeout(120000);

而对于2.x和更高itbeforeEach等区块预计,以达到规范范围内定期的功能。 如果应该到达套件上下文( describe this ),则可以将其分配给另一个变量:

describe('...', function () {
  const suite = this;

  before(function () {
    // common suite timeout that doesn't really need to be placed inside before block
    suite.timeout(60000); 
  }); 
  ...
  afterEach(function (done) {
    this.timeout(120000);
    // ...
    done();
  });
});

由于规范上下文很有用,因此希望像这样使用Mocha上下文,并且实际上没有充分的理由在规范内部访问套件上下文。

对于异步块, done参数或promise返回是必需的。

如果需要使用动态上下文,则必须使用常规功能。

describe('Resources', function () {
  // ...
  afterEach(function (){
    this.timeout(120000)  // this should work
    // ... delete any created resources ...
  })
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM