I have
axios.post = jest.fn().mockResolvedValue(< responseData >)
but I need the response data to change depending on what is in the body of the request. How can I capture it, please? Something like
axios.post = jest.fn().< get the body >.mockResolvedValue(< responseData based on the body >)
You are looking for mockImplementation
, then you can customize the response data based on request payload.
e.g
import axios from 'axios';
describe('78936100', () => {
test('should pass', async () => {
axios.post = jest.fn().mockImplementation((url, data) => {
if (data.id === 1) {
return { data: 'a' };
}
if (data.id === 2) {
return { data: 'b' };
}
});
const res1 = await axios.post('http://localhost:8080', { id: 1 });
expect(res1.data).toEqual('a');
const res2 = await axios.post('http://localhost:8080', { id: 2 });
expect(res2.data).toEqual('b');
});
});