Open alyssaruth opened 3 years ago
Hi,
thanks for reporting - I was able to reproduce this with the latest version of create-react-app / react-scripts. It looks like this is related to react-scripts
defaulting jest's resetMocks
to true
since v4. Consequently you can workaround this bug by adding:
"jest": {
"resetMocks": false
}
in your package.json
while I am further investigating this issue.
Thanks for looking into this and responding so quickly with a workaround! :tada:
I also ran into this issue after the CRA 4.0 upgrade, and I can confirm that the resetMocks: false
workaround worked for our project as well.
Hmm, the resetMocks: false
workaround doesn't seem to be working here - I'm still seeing Error: No request to respond to!
Hi,
there are several upstream issues for the resetMocks
behavior breaking existing test behavior (e.g. https://github.com/facebook/create-react-app/issues/9935). I'm still looking for a feasible solution except for the aforementioned workaround.
@alexburlton-sonocent I created a repro-repo using the example code you provided at kingjan1999/jest-mock-axios-cra-repo. Can you try if this works for you and look for differences between this repo and your project?
Sorry for going AWOL, the create-react-app upgrade dropped off our radar for a bit so I've not been looking at this for a while.
Dug into this a bit more today and have concluded that it's our use of react-app-rewired which is breaking the workaround. If I change our test command to go via vanilla react-scripts
rather than react-app-rewired
, I can indeed see that specifying the resetMocks
option fixes the problem.
So now I need to figure out if we can drop react-app-rewired
for running tests, or failing that how to make it respect the resetMocks
option. Have you made any progress on a feasible solution for making these tests work with resetMocks
set to true?
Not sure if related, but I am getting Cannot set properties of undefined (setting 'Authorization')
after upgrading to Jest 28. This comes from my custom axios configuration that sets the auth token:
const setAuthConfig = (config) => {
const token = localStorage.getItem('access');
if (token) { config.headers.Authorization = `Bearer ${token}`; }
return config;
};
TypeError: Cannot set properties of undefined (setting 'Authorization')
18 | const token = localStorage.getItem('access');
19 |
> 20 | if (token) { config.headers.Authorization = `Bearer ${token}`; }
| ^
21 |
22 | return config;
23 | };
Setting resetMocks: false
doesn't help with this issue for me though
@doutatsu Can you post a more complete example that shows how you invoke setAuthConfig
/ axios? (maybe in a separate issue?)
@doutatsu did you ever resolve this issue? I'm facing the exact same problem
I don't use Jest anymore, I've switched to Vitest
Hi!
We're using this library in a project built with Create React App, and have followed the async / await testing pattern outlined in https://github.com/knee-cola/jest-mock-axios/issues/28.
I'm trying to upgrade us to CRA 4, but doing so is breaking all of these tests with
Error: No request to respond to!
. Bumping major CRA version bumps a load of sub-dependencies, so guessing one of these is the actual cause (jest seems most likely) - I'm happy to check other package versions before/after the upgrade if there are any in particular you think will be relevant!We were using version
2.3.0
of this library, but can also reproduce the issue on the latest version (4.2.1
). I've put together a SRE of three files to demonstrate what's happening.Our axios instance, defined in
axiosInstance.ts
A fake API to test in
sreApi.ts
The test that fails after the upgrade, but passes before in
sreApi.test.ts
In the above example, the
mockResponse
line blows up with:Interestingly, reordering the test like this still fails but does pass the call assertion, so
mockAxios.post
is definitely still being called:Versions that work:
jest
:24.9.0
react-scripts
:3.4.4
axios
:0.18.1
jest-mock-axios
:4.2.1
Versions that don't work (after upgrade):
jest
:26.6.0
react-scripts
:4.0.1
axios
:0.18.1
jest-mock-axios
:4.2.1