knee-cola/jest-mock-axios

mock axios using axios interceptors

Closed this issue · 4 comments

I'm trying to use mockAxios for testing with axios interceptors.

image

image

When I created mockAxios:
export default {
get: jest.fn(() => Promise.resolve(data: {}))
}
All of my tests failed with the follow message: cannot read property response of undefined inside of axios interceptors. It happens because mock axios doesn't return response. It could just return a plain object. So how can I use axios interceptors with mockAxios for testing?
image

I used this in my __mocks__/axios.js file:

import mockAxios from 'jest-mock-axios';
export default {
    ...mockAxios,
    create: jest.fn(() => ({
        ...mockAxios,
        defaults: {
            headers: {
                common: {}
            }
        },
        interceptors: {
            request: {
                use: jest.fn()
            },
            response: {
                use: jest.fn()
            }
        }
    }))
};```

@marvinkome works smooth! Thanks.

import mockAxios from 'jest-mock-axios';
export default {
    ...mockAxios,
    create: jest.fn(() => ({
        ...mockAxios,
        defaults: {
            headers: {
                common: {}
            }
        },
        interceptors: {
            request: {
                use: jest.fn()
            },
            response: {
                use: jest.fn()
            }
        }
    }))
};

Didn't help. Still have the same issue.

That's a duplicate of #5.