Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling reset does not reset the axios mock function #19

Open
LukeSilva opened this issue Oct 17, 2018 · 3 comments
Open

Calling reset does not reset the axios mock function #19

LukeSilva opened this issue Oct 17, 2018 · 3 comments

Comments

@LukeSilva
Copy link

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
Copy link

drwpow commented Nov 30, 2018

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
Copy link

kmarple1 commented Jun 6, 2022

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants