我正在使用该模块rn-fetch-blob
来管理 React-Native 项目中的下载。该模块提供了一个StatefulPromise
类,它基本上是一个带有一些附加功能的承诺。这个添加的功能给我的 Jest 单元测试带来了问题。这是最初导致问题的模拟(文件中的代码__mocks__/rn-fetch-blob.js
):
export default {
config: jest.fn(x => {
return {
fetch: jest.fn(x => Promise.resolve({ // <- I believe the problem lies here
info: () => {
return {status: 200}
}
}))
}
})
}
在我的代码中的某处,我引用了 StatefulPromise 方法之一 ( expire
)。涵盖该方法的单元测试失败并显示以下消息:
filePromise.expire is not a function
.
我的想法是为了测试而创建自己StatefulPromise
的:
class StatefulPromise extends Promise {
expire() {
jest.fn(() => {})
}
}
...
...
...
fetch: jest.fn(x => StatefulPromise.resolve({<same logic as before>});
这并没有解决问题。
我会注意到这在我的浏览器 javascript 控制台中似乎工作得很好。以下内容也让我感到惊讶:
let foo = StatefulPromise.resolve('foo');
console.log(foo instanceof StatefulPromise);
// true in console; false in jest
这让我很困惑。有没有更好的方法来解决这个问题?