{ return new Promise(asyn-6ren">
gpt4 book ai didi

javascript - 在 mocha 中断言函数失败 "assert"导致超时而不是失败

转载 作者:行者123 更新时间:2023-11-28 20:14:17 26 4
gpt4 key购买 nike

我有以下测试:

it ('clears the many cache when a new document is inserted', () => {

return new Promise(async(resolve) => {
const entity = new Entity(connector, collectionName);
entity.insert({_id: '2'});
result = await entity.findManyCached();

// this assert does not fail the test, but makes the case time out
assert.deepEqual(result, [{_id: 1}]);
})
})

如何收集失败断言的结果而不是超时?

最佳答案

我认为发生的事情是,由于使 promise 的执行程序函数异步,您将得到与此等效的内容:

return new Promise(outerResolve => {
return new Promise(innerResolve => {
...
assert.deepEqual(...);
})
})

assert 抛出的任何异常都会被内部 promise 吞没,并且外部 promise 永远不会被解决被拒绝,因此超时。

独立演示:

let p = new Promise(outerResolve => {
console.log('outer promise');
return new Promise(innerResolve => {
console.log('inner promise');
throw Error('foo');
})
})

// This doesn't log anything:
p.then (x => console.log('resolved'))
.catch(e => console.log('rejected'));

解决方法如下:

it ('clears the many cache when a new document is inserted', async () => {
const entity = new Entity(connector, collectionName);
entity.insert({_id: '2'});
let result = await entity.findManyCached();

// this assert does not fail the test, but makes the case time out
assert.deepEqual(result, [{_id: 1}]);
})

关于javascript - 在 mocha 中断言函数失败 "assert"导致超时而不是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39405777/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com