【问题标题】:How to set timeout in Mocha root-level hooks?如何在 Mocha 根级挂钩中设置超时?
【发布时间】:2021-04-04 06:38:07
【问题描述】:

我想为 Mocha 测试的根级挂钩设置超时。

我的根级钩子看起来像这样,在test/setup.ts:

import * as database from '../src/database/_db'

export const mochaHooks = {
    /* Connect to the DB before running any tests */
    beforeAll(done: () => void) {
        database.initialize(done)
    },
    
    /* Close the DB connect after all tests finished */
    afterAll(done: () => void) {
        database.close(done);
    }
};

这是通过--require test/setup.ts 加载的。数据库连接需要很长时间,所以我想增加超时值。

使用 -t 10000 运行 Mocha 可以,但也会为每个测试设置超时。

我也尝试在BeforeAll 中添加this.timeout(3000),但得到了✖ ERROR: test/setup.ts:10:14 - error TS2339: Property 'timeout' does not exist on type '{ beforeAll(done: () => void): void; afterAll(done: () => void): void; }'.

【问题讨论】:

    标签: javascript node.js typescript mocha.js


    【解决方案1】:

    来自文档ARROW FUNCTIONS

    不鼓励将箭头函数(又称“lambdas”)传递给 Mocha。 Lambda 词法绑定 this 并且无法访问 Mocha 上下文。

    使用箭头函数时未绑定测试上下文。所以你不能使用this.timeout()

    您应该将箭头函数更改为由function 关键字组成的function declaration

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-10
      • 2021-06-03
      • 2021-02-11
      • 1970-01-01
      • 2021-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多