I'm using the module rn-fetch-blob
to manage downloads in a React-Native Project. The module provides a StatefulPromise
class which is basically a promise with some added functionality. This added functionality is creating problems for my Jest unit tests. Here's the mock that had originally caused the problem (code within the file __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}
}
}))
}
})
}
Somewhere within my code I had made a reference to one of the StatefulPromise methods (expire
). Unit tests covering that method are failing with this message:
filePromise.expire is not a function
.
My thinking was to then create my own StatefulPromise
for the sake of the tests:
class StatefulPromise extends Promise {
expire() {
jest.fn(() => {})
}
}
...
...
...
fetch: jest.fn(x => StatefulPromise.resolve({<same logic as before>});
This did not solve the problem.
I will note that this appears to work just fine in my browsers javascript console. The following surprised me as well:
let foo = StatefulPromise.resolve('foo');
console.log(foo instanceof StatefulPromise);
// true in console; false in jest
This has left me quite confused. Is there a better way to approach this problem?