每次之后的Mocha更改超时 [英] Mocha change timeout for afterEach

查看:120
本文介绍了每次之后的Mocha更改超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a node application with mocha and chai. Some of the tests call an external API for integration tests, which might take up to 90sec to perform the action.

为了正确清理,我定义了一个afterEach() -block,它会删除所有生成的远程资源,以防万一期望失败并且某些资源没有被删除.

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.

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

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)

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

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:

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

Any hints? Mocha is version 4.0.1, chai is 4.1.2

推荐答案

所有Mocha块的规则都相同.

The rules are same for all Mocha blocks.

timeout:

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

对于2.x和更高版本的itbeforeEach等,块应该是常规功能,以便达到规格上下文.如果应该到达套件上下文(describe this),则可以将其分配给另一个变量:

And for 2.x and higher it, beforeEach, etc. blocks are expected to be regular functions in order to reach spec context. If suite context (describe this) should be reached, it can be assigned to another variable:

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上下文,并且实际上没有充分的理由在规范内部访问套件上下文.

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.

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

And done parameter or promise return are necessary for asynchronous blocks.

这篇关于每次之后的Mocha更改超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆