[英]Jest/SuperTest how to correctly expect Socket Hangup across a set of promises?
我有一个测试,上面写着“在大约 X 个并发连接之后,我应该看到套接字挂断,因为我的服务将停止回答有人锤我。”
经过大量痛苦的试验和错误后,这工作得很好,但是因为我第一次看到挂断时使用的是await Promise.all(myrequests)
,它会引发socket hang up
异常。
这可行,但会导致一些错误消息,因为我的挂机例程会进行一些调试日志记录,此时测试已经结束。
最好的说法是:“等待所有这些,即使它们抛出错误?”
我的笑话/超级测试问题块看起来像:
//Send enough concurrent connections to trip the dropout
for(var i = 0;MAX_CONCURRENT_CONNECTIONS+5;i++)
{
requests.push(request(app).get('/'))
}
//wait for all the connections to resolve
const t = async () => {
await Promise.all(requests);
};
//and make sure some drop off
expect(t).toThrow("socket hang up"); //this also doesn't match the error string right but that is because I'm not as experienced in this sort of thing as I'd like!
//however after this, the test ends, and my back end logging causes problems since the test is over!
即使抛出await Promise.all(requests)
,仍然等待请求中所有承诺的最佳方法是什么?
我可以做以下丑陋的代码,但我正在寻找正确的方法来写这个:)
let gotConnReset = false
try
{
await Promise.all(requests);
}
catch(err)
{
if(err.message == "socket hang up")
{
gotConnReset = true;
}
}
assert(gotConnReset === true);
//wait for all the other requests so that Jest doesn't yell at us!
await Promise.allSettled(requests);
我不知道 Jest 有什么可以帮助的,但有Promise.allSettled
将等待所有承诺履行或拒绝,返回所有结果的数组。 被拒绝的承诺将附加一个.reason
。
主要区别在于 Jest 将匹配错误对象而不是使用抛出的错误匹配器,因此某些特定于错误的功能不存在。
test('allSettled rejects', async () => {
class Wat extends Error {}
const resError = new Wat('Nope')
resError.code = 'NO'
const res = await Promise.allSettled([
Promise.resolve(1),
Promise.reject(resError),
Promise.resolve(3),
])
expect(res).toEqual(
expect.arrayContaining([
{ status: 'rejected', reason: new Error('Nope') },
])
)
})
✓ allSettled rejects (2 ms)
如果您想避免上面示例的“松散”匹配,如果message
匹配,则通过任何Error
object,它可能需要类似jest-matcher-specific-error或expect.extend
错误匹配器
结果可以使用filter
/ map
或直接reduce
到测试拒绝。
(await Promise.allSettled())
.filter(o => o.status === 'rejected')
.map(o => o.reason)`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.