Closed KeynesYouDigIt closed 2 years ago
Hi,
thanks for raising this issue. Can you do console.log(axioms);
or similar, to make sure the axioms instance is infact mocked correctly?
Sure thing!
looks like it is to me
console.log
HELLO INSIDE COMPONENT
at MyComponent.handleSave (/MyComponent.js:307:13)
console.log
[Function: mockConstructor] {
....
Let me know if you'd like the full output. I will mess around in the debugger and add any interesting data I can find.
This is what I see in the debug console.... odd.... (breakpoint is inside the component function that uses axios)
axios
{default: ƒ, __esModule: true}
axios.post
undefined
Can you try going to v4.5.0?
Same error I am afraid, thanks so much for helping me look in to this
What does your import look like?
// __mocks__/axios.js
import mockAxios from 'jest-mock-axios';
export default mockAxios;
// someTest.test.js
import mockAxios from 'jest-mock-axios';
That's strange. I'll try to look into this on the weekend
Much appreciated! I couldnt get axios-mock-adapter to work either, nor my own mocks, so it may well not be an issue with this particular library. Perhaps having an axios call nested in a component method is not something commonly tested this way? Its also possible my jest env is mucking something up, I just don't know what.
Aha,
// package.json
"jest": {
"resetMocks": false
},
Appears to change the behavior to work... which is odd because false is the default? https://jestjs.io/docs/configuration#resetmocks-boolean
We are running older versions of several libraries, so I suspect the issue is there. thanks for the help! sorry that this was not an issue with the library, I might turn it in to a stack overflow thread.
Thanks for finding this out! This is very interesting, as the behavior used to be different when resetMocks
was true
(as described in #63)
@KeynesYouDigIt's snippet worked for me, thanks, saved me an hour of debugging. Not sure why though.
Version: "jest-mock-axios": "^4.6.1"
I have a react component that binds a function to an
onClick
eventand it makes an http call
I set up
mockAxios
as suggested by the read me. For some reason, when I.simulate('click')
on the button node, I get this crap, like post didnt return a promise?Does anyone know if axios being nested in a function means the mock wont work? or is there another thread to tug at?