knee-cola/jest-mock-axios

Calling reset does not reset the axios mock function

Opened this issue · 3 comments

You can call axios with code like the following:

 method: 'post',
 url: '/user/12345',
 data: {
   firstName: 'Fred',
   lastName: 'Flintstone'
 }
});```

This code is correctly mocked, and you can test it fine.
However calling axios.reset() will not reset the mock state for these functions

I’m experiencing something similar, but this might be related to #17.

I’m trying to test mockResponse with mockError in the same suite. If I call mockResponse first, then the first and all subsequent calls will work fine, but mockError will err with Cannot read property 'reject' of undefined.

The opposite is true: if I call mockError first then try and call mockResponse. The error call will execute fine but subsequent calls will err with Cannot read property 'resolve' of undefined.

I’m calling mockAxios.reset() on afterEach every time.

Edit: the axios call I’m testing uses both .then() and .catch(). Code here

I'm having the same issue with jest-mock-axios 4.6.1. I've got mockAxios.reset() being called in afterEach(), and I've verified that it's actually being called every time. However, if I put an expect(mockAxios).not.toHaveBeenCalled() at the beginning of one of my later test, it will fail with multiple calls from previous tests in the same file.

For me (4.5.0), adding

beforeEach(() => {
    jest.clearAllMocks()
})

on top of

afterEach(() => {
    mockAxios.reset()
})

works for me. Not ideal as the example snippet provided which causes confusion, but it's working.