knee-cola / jest-mock-axios

Axios mock for Jest
252 stars 42 forks source link

Calling reset does not reset the axios mock function #19

Open LukeSilva opened 6 years ago

LukeSilva commented 6 years ago

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
drwpow commented 5 years ago

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

kmarple1 commented 2 years ago

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.

taiweituan commented 2 years ago

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.