'Jest manual mock for axios not able to override the POST method from one of the test suite

In the JavaScript file, I am having the logic below

const {
       data: { token: token }
      } = await axios({
                 method: 'post',
                 url: `XXX/xxx`,
                 headers: {},
                data: data
         });

I have created a manually mock file in the folder __mocks__ folder as axios.js and has below

jest.mock('axios');

module.exports = config => {
    const url = config.url;
    if (config.method === 'post') {
        if (
            url.includes('XXX')
        ) {
            return Promise.resolve({
                data: data
            });
        } else {
            return Promise.reject(new Error(`Invalid URL`));
        }
    } else if (config.method === 'get') {
        if (url.includes('YYY')) {
            return Promise.resolve({
                data: data
            });
        } else {
            return Promise.reject(new Error(`Invalid URL`));
        }
    }
};

From the actual token.test.js, i have imported the axios

let axios = require('axios');

In one of the test suites, I am not able to override the axios. For example, I want to override the POST call of URL to reject with "Network Error". But I am not able to achieve it. Can anyone please help me on this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source