jefflau / jest-fetch-mock

Jest mock for fetch
MIT License
882 stars 116 forks source link

Test Failure After Upgrading to CRA 4.0 #184

Closed Bangertm closed 3 years ago

Bangertm commented 3 years ago

After upgrading react-scripts to 4.0, I'm seeing an issue jest-fetch-mock in tests that previously passed. In particular calling fech is raising:

TypeError: isMocking is not a function or its return value is not iterable

from this line: https://github.com/jefflau/jest-fetch-mock/blob/6c1f4bc78c12eda4437f4c761a934a72a3648e02/src/index.js#L100

react-scripts 4.0 introduces Jest 26 and a default of setting resetMocks to true. I have found that setting resetMocks to false will fix the issue. Ideally I would be able to leave resetMocks in its default.

yinzara commented 3 years ago

Unfortunately jest's automatic "resetMocks" functionality completely breaks how jest-fetch-mock works.

To fix, you either can disable resetMocks as you suggested or call "fetchMock.resetMocks()" in a "beforeEach()".

We'll have to add some additional documentation about this.

gerhardsletten commented 3 years ago

Also looks like you need to add fetch.dontMock in the beforeEach (if you only want to override moch some fetches):

beforeEach(async function () {
  fetch.resetMocks()
  fetch.dontMock()
})

even if you have it in the global setupFiles for jest

require('jest-fetch-mock').enableMocks()
fetch.dontMock()
dreamerchandra commented 3 years ago

In CRA 4.0.1 this issue is happening... Pls Update the doc on how to resolve this... I can't find a way-around... Solutions discussed here aren't working with fetch. mockResponseOnce. However fetch.mockResolvedValueOnce seems to work

So I'm currently using like this fetch.mockResolvedValueOnce({ json: () => ({ data: 'res' }) })

ericchernuka commented 3 years ago

Just following up on this to see if anyone was able to resolve this easily? I'm hitting this pretty hard right now.

zigasinko commented 3 years ago

There must be something else wrong with using jest-fetch-mock in create-react-app >4.0 (probably an issue with Jest 26 which was updated in latest version of create-react-app. For me everything fetch related comes back undefined. Except if I put in fetchMock.dontMock(); which means fallback to default fetch API and that's not a solution.

MarkusRohlof commented 3 years ago

For anyone else who might have been confused for the better part of an hour about how to set resetMocks to false with a create-react-app project, just add a "jest" section to your package.json:

...
"devDependencies": {
  ...
  "jest-fetch-mock": "3.0.3"
},
"jest": {
  "resetMocks": false
}
yinzara commented 3 years ago

Just following up on this to see if anyone was able to resolve this easily? I'm hitting this pretty hard right now.

Have you tried setting the "resetMocks" to false in the Jest config like suggested?

yinzara commented 3 years ago

I'm going to close this issue as I've now updated the README.md with instructions on setting this Jest configuration and why it happened.

ilivestrong commented 3 years ago

thank you so much! After 2 hours of Googling, found this thread. <3