在摩卡中,我可以断言请求超时吗?

In Mocha can I assert that a request times out?

我正在测试/确认一些数据库锁定内容,我想进行一项测试,以确认如果我锁定该行,请求("request-promise" 库)将挂起。

一些行不通的想法是

request(options).catch((response) => {})

assert.throws(fn, Error, "timeout")

我继续收到此消息:Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

有没有办法断言超时发生了?

在 mocha 测试(或 describebefore 等)中,this.timeout(msec) 将更改在 Mocha 考虑测试失败。

查看 Mocha documentation 了解更多信息。

我的解决方案是将超时时间设置为我预期超时时间的一半。

setTimeout(function(){ done(); })

然后在我的端点承诺响应中

done(new Error("Timeout should have happened.");

这样,如果发生超时,就会调用 done。如果没有发生,则调用 done 时出错。