使用catch in promise进行断言超过超时时间
Making assertions with catch in promises exceeds timeout
我想在promise链的catch
块中进行断言,但它达到了超时。断言在then
块中起作用,但在catch
块中,似乎从未达到done()
。它被压制了吗?有没有更好的方法来测试拒绝承诺的情况?
import assert from 'assert';
import { apicall } from '../lib/remoteapi';
describe('API calls', function () {
it('should test remote api calls', function (done) {
apicall([])
.then((data) => {
assert.equal(data.items.length, 2); // this works fine
done();
})
.catch((e) => {
console.log('e', e);
assert.equal(e, 'empty array'); // ?
done(); // not reached?
});
});
});
拒绝承诺
apicall(channelIds) {
if(channelIds.length === 0) return Promise.reject('empty array');
...
}
我得到这个错误:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
如果这是摩卡,并且您正在使用Promises,请不要使用回调功能;正如你所发现的那样,这会让事情变得非常尴尬。相反,摩卡让你在测试中返回承诺。被拒绝的Promise意味着测试失败,成功的Promise则意味着测试成功。失败的断言会导致代码抛出异常,这将自动使Promise失败,这通常是您想要的。简而言之:
describe('API calls', function () {
it('should test remote api calls', function () {
return apicall([])
.then((data) => {
assert.equal(data.items.length, 2);
});
});
});
相关文章:
- 如何设置$.ajax请求的超时,并在超时时间过长时进行重做
- jasmine 2-在jasmine指定的超时时间内未调用异步回调.DEFAULT_TIMEOUT_INTERVAL
- set超时,超时时间较短,超时时间较长
- 设置函数的超时时间
- 在警报框上设置超时时间
- 设置最小时间和超时时间
- 摩卡,超过2000ms的超时时间.确保在这个测试中调用了done()回调
- 设置屏保超时时间
- For循环在超时时间内具有动态数字
- 反复调用一个函数,直到它的承诺被解决,或者直到设置的超时时间过去
- 设置加载apache的超时时间
- 我无法在Selenium独立上编辑超时时间
- 在Node.js中设置HTTP超时时间
- 当XMLHttpRequest处于“pending”状态时;状态(在Chrome中)是否将该时间计入它的超时时间
- 如何设置浏览器创建的桌面通知的关闭超时时间?
- 在Div背景中加载图像时设置超时时间
- 设置ondrop函数的超时时间
- Jquery在弹出窗口设置超时时间
- 设置超时时间打开2个窗口
- 使用catch in promise进行断言超过超时时间