jefflau/jest-fetch-mock

Test Failure After Upgrading to CRA 4.0

Bangertm opened this issue ยท 9 comments

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:

const [mocked, request] = isMocking(input, reqInit)

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.

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.

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()

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' }) })

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

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.

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
}

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?

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.

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