Closed Shub1nk closed 3 months ago
Facing same issue. Does anyone had that and fixed it or do I need to replace library for mocking axios?
I found out when I run mock in Storybook then all work. I made example with mocking inside this library and this example work too.
But, when I run Jest when I use axios-mock-adapter inside tests then new MockAdapter
begin to return Promise and I get error mock.onGet is not a function
.
I don't understand what is going on.
Must be Jest specific. I have axios 1.5 working with axios-mock-adapter
@marcbachmann Can you share your Jest config?
I have next settings in Jest for axios:
transformIgnorePatterns: ['/node_modules/(?!axios)'],
moduleNameMapper: {
axios: '<rootDir>/node_modules/axios/dist/node/axios.cjs',
},
People from other releases write that jest, axios and axios-mock-adapter work for them with these settings. For some reason it doesn't work for me
sorry, not using jest. that's why it must be jest specific 😅
it works with axios 0.27.2. When I was updated axios to 1.5 it broke
I've manage to fix it in my case by bumping versions of jest and ts-jest. My current versions are:
Thanks @marcbachmann for pointing out the right direction 😄
I created test repo with example https://github.com/Shub1nk/test-jest-and-axios-mock-adapter
I use jest@29, axios@1.5 and axios-mock-adapter@1.22.0 and it work. But if I enable these options in jest's config, I will get error that mock.onGet is not function
(mock -> Promise) again.
// ATTENTION: Test with axios mock fails if I enable these options
transformIgnorePatterns: ['/node_modules/(?!axios)'],
moduleNameMapper: {
axios: '<rootDir>/node_modules/axios/dist/node/axios.cjs',
},
Multiple libraries are affected due to axios, I tried with msw, nock and this one none of them is working properly axios@1.5.0
Same issue with axios@1.6.0
Looks like changing moduleNameMapper
from axios
to ^axios$
makes it work:
moduleNameMapper: {
'^axios$': 'axios/dist/node/axios.cjs',
}
I tried to update
axios
to1.2.4
in my project three months ago. But it failed.When I launched
Jest
, I got one of these errors:onGet/onPost/reset is not defined
orError: connect ECONNREFUSED 127.0.0.1:80
I found some discussions, where was written that metod
getAdapter
is not public in the new version of axios.https://github.com/ctimmerm/axios-mock-adapter/issues/355)](https://github.com/ctimmerm/axios-mock-adapter/issues/355
https://github.com/axios/axios/issues/5474)](https://github.com/axios/axios/issues/5474
Method
getAdapter
was made public in axios 1.5.0. In the lastest version ofaxios-mock-adapter
version of axios is0.27.2
.Do I understand correctly that this library can't work with projects, which have
axios
version higher than0.27.2
?If it's true, do you plan to fix this problem?
Or am I doing something incorrectrly?
My project dependecies:
My test example:
I have this error: