knee-cola/jest-mock-axios

Always going to catch function if we write catch block.

naveenkanumoori opened this issue ยท 5 comments

If we have both then() and catch() then no matter what we mock as a response, control is going to catch() only.
If we dont write catch() then it is working fine, but I need to handle error response in order to display error message.

Can you please help me with that?

I was running into this when using the base jest-mock-promise library, I think its fixed by this PR that's open but we're wating for the maintainer to merge it

As a temporary workaround I've added this

const mockAxios = require('jest-mock-axios');

mockAxios.default.mockResponse = function(res) {
    const response = Object.assign({
        data: {},
        status: 200,
        statusText: 'OK',
        headers: {},
        config: {}
    }, res);

    const promise = this.popRequest().promise;

    promise.handlers.sort((a, b) => {
        const aScore = a.hasOwnProperty('catch') ? 0 : 1;
        const bScore = b.hasOwnProperty('catch') ? 0 : 1;

        return bScore - aScore;
    });

    promise.resolve(response);
};

module.exports = mockAxios;

to my __mocks__/axios.js file and it works.

Is there any news on this issue? We cannot use jest-mock-axios without proper catch() handling.

Can you help me out here with a minimal example reproducing the bug described?
I tried the following code:

const makeWithCatch = (thenFunc: () => void, catchFunc: () => void) => {
  const promise = axios.get("/test/");
  promise.then(thenFunc).catch(catchFunc);
  return promise;
};

describe("catch / then", () => {
  it("calls then when then should be called", () => {
    const thenFunc = jest.fn();
    const catchFunc = jest.fn();

    makeWithCatch(thenFunc, catchFunc);
    expect(mockAxios.get).toHaveBeenCalled();

    mockAxios.mockResponse({ data: "test" });

    expect(catchFunc).not.toHaveBeenCalled();
    expect(thenFunc).toHaveBeenCalled();
  });
});

But this works fine. However, I think there is in fact a bug with the catch handling here (at least the PR linked above seems quite valid), I'm for the moment just unable to reproduce in a way that applying the PR solves the problem. In any case, if there is really an upstream bug with jest-mock-promise, we should consider switching to synchronous-promise.

Should be fixed with 3.1.0 (just published) as I switched the promise library. Please report back if it still doesn't work.