[英]How to test (using Jest) a function that returns a promise that contain another promise
So, I want to test (using Jest) a function which returns a promise that contains an axios request (as another nested promise) and resolve the outer promise within then
and catch
blocks.
function与此类似:
fetchFunction() {
return new Promise((resolve, reject) => {
if (!this.valid) {
this.value = 1;
return resolve();
}
this.someModel.$get()
.then(() => {
this.value = 2;
return resolve();
})
.catch(e => {
this.value = 3;
return reject(e);
});
});
},
如果您在谈论单元测试,则不应在同一个测试中同时测试两者。
即您对 fetchFunction() 方法的测试将有一部分在其中模拟this.someModel.$get()
function
您可以创建一个模拟 function ,它返回一个 promise ,例如:
jest.fn().mockImplementation(() => return Promise.resolve('mock'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.