摩卡更改afterEach超时
我正在用摩卡和柴编写一个节点应用程序。 某些testing调用外部API进行集成testing,这可能需要90秒才能执行此操作。
为了正确清理,我定义了一个afterEach()
– 块,它将删除任何生成的远程资源,以防期望失败,并且一些资源没有被删除。
testing本身有一个增加的超时,而其余的testing应保持其默认和小超时:
it('should create remote resource', () => {...}).timeout(120000)
然而,我不能用afterEach().timeout(120000)
做同样的afterEach().timeout(120000)
,因为这个函数不存在 – 由于未知的资源名称,我也不能使用function ()
notation:
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) })
任何提示? 摩卡是版本4.0.1,柴是4.1.2
所有摩卡块的规则都是一样的。
可以在Mocha 1.x中为箭头函数设置timeout
:
afterEach((done) => { // ... done(); }).timeout(120000);
而对于2.x及更高版本, beforeEach
等块预计是规则的function,以达到规范的上下文。 如果套件上下文( describe
this
)应该达到,它可以被分配给另一个variables:
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(); }); });
摩卡的上下文预计将被这样使用,因为规范上下文是有用的,并且实际上没有充分的理由来访问规范内的套件上下文。
并且done
参数或promise返回对于asynchronous块是必需的。
如果你需要使用dynamic上下文,你必须使用正常的function。
describe('Resources', function () { // ... afterEach(function (){ this.timeout(120000) // this should work // ... delete any created resources ... }) })