I know that npm request module is now deprecated, but i want to mock a post http call with jest.
Here is my function
import { post } from 'request';
export functionToFetch(uriFetching) {
return post(
{
url: uriFetching,
headers: {},
json,
},
(error, response, body) => {
if (error) {
console.log('error)
// return specific code
}
// return success code
}
}
And in try this in my test :
import { post } from 'request';
import {functionToFetch} from './example';
it('should do some specific handling on error', () => {
const fakeURI = 'http://example.com'
request.post = jest.fn().mockImplementation(() => Promise.resolve(new Response('test')));
// on error
expect(functionToFetch(fakeURI).toEqual(expected);
// on success
expect(functionToFetch(fakeURI).toEqual(expected2);
});
But it returns TypeError: Cannot set property 'post' of undefined
I want to mock that method to handle error and response to test methods inside them