简体   繁体   English

每次之后的Mocha更改超时

[英]Mocha change timeout for afterEach

I am writing a node application with mocha and chai. 我正在用mocha和chai编写一个节点应用程序。 Some of the tests call an external API for integration tests, which might take up to 90sec to perform the action. 一些测试调用用于集成测试的外部API,这可能最多需要90秒才能执行操作。

In order to cleanup properly, I defined an afterEach() -block, which will delete any generated remote resources, in case an expect fails and some resources weren't deleted. 为了正确进行清理,我定义了afterEach() - afterEach() ,如果期望失败并且某些资源没有被删除,它将删除所有生成的远程资源。

The tests themselves have an increased timeout, while the rest of the tests should retain their default and small timeout: 测试本身会增加超时,而其余测试应保留其默认值和较小的超时:

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

However, I can't do the same with afterEach().timeout(120000) , because the function does not exist - nor can I use the function () -notation due to the unknown resource names: 但是,我无法对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)
})

Any hints? 有什么提示吗? Mocha is version 4.0.1, chai is 4.1.2 Mocha是4.0.1版,chai是4.1.2版

The rules are same for all Mocha blocks. 所有Mocha区块的规则都相同。

timeout can be set for arrow functions in Mocha 1.x with: 可以使用以下命令在Mocha 1.x中为箭头功能设置timeout

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

And for 2.x and higher it , beforeEach , etc. blocks are expected to be regular functions in order to reach spec context. 而对于2.x和更高itbeforeEach等区块预计,以达到规范范围内定期的功能。 If suite context ( describe this ) should be reached, it can be assigned to another variable: 如果应该到达套件上下文( 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 contexts are expected to be used like that, since spec context is useful, and there are virtually no good reasons to access suite context inside specs. 由于规范上下文很有用,因此希望像这样使用Mocha上下文,并且实际上没有充分的理由在规范内部访问套件上下文。

And done parameter or promise return are necessary for asynchronous blocks. 对于异步块, done参数或promise返回是必需的。

If you need to use dynamic context you have to use normal function. 如果需要使用动态上下文,则必须使用常规功能。

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