I'm having issues testing my thunks, as many of my API calls are using FormData, and I can't seem to figure out how to mock this in tests. I'm using Jest.
My setup file looks like this:
import 'isomorphic-fetch';
// Mocking the global.fetch included in React Native
global.fetch = jest.fn();
// Helper to mock a success response (only once)
fetch.mockResponseSuccess = body => {
fetch.mockImplementationOnce(() =>
Promise.resolve({ json: () => Promise.resolve(JSON.parse(body)) })
);
};
// Helper to mock a failure response (only once)
fetch.mockResponseFailure = error => {
fetch.mockImplementationOnce(() => Promise.reject(error));
};
However, I get the following error on all tests that require FormData:
ReferenceError: FormData is not defined
I've tried importing the FormData file from react-native-mock
, under src/Libraries/Network/FormData
, but it didn't work.
So I was wondering if anyone has had any luck doing this?
In general, I'm having a hard time figuring out the best way to mock fetch
requests in React Native, so any advice here would be nice. I've tried the jest-fetch-mock
lib (and opened an issue about FormData), tried setting up with nock
(no luck), and this plain Jest implementation, but nothing feels right yet.