所以,我想测试(使用 Jest)一个函数,该函数返回一个包含 axios 请求的承诺(作为另一个嵌套承诺)并解析 then
和 catch
块内的外部承诺。< /p>
功能类似这样:
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);
});
});
},
答案 0 :(得分:1)
如果您在谈论单元测试,则不应在同一个测试中同时测试两者。
即您对 fetchFunction() 方法的测试将有一部分用于模拟 this.someModel.$get()
函数
您可以创建一个模拟函数,它返回一个像 e thant 这样的承诺:
jest.fn().mockImplementation(() => return Promise.resolve('mock'))