jefflau / jest-fetch-mock

Jest mock for fetch
MIT License
886 stars 118 forks source link

Unexpected end of JSON input with CRA #63

Open mladenp opened 6 years ago

mladenp commented 6 years ago

I am using this with Create-React-App and react-app-rewired but i am getting error on running tests:

...node_modules/react-scripts/scripts/test.js:20
  throw err;
  ^

SyntaxError: Unexpected end of JSON input
    at parse (<anonymous>)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:182:7)
npm ERR! Test failed.  See above for more details.
jefflau commented 6 years ago

What does your project and setup look like?

BrunoBernardino commented 6 years ago

I believe this happens when you do response.json() for a request to a URL that doesn't return a Content-Type: application/json header (but something like text/html) — which works in browser's fetch, but not in the mock.

mistricky commented 3 years ago

Same problem, any workaround here?

BrunoBernardino commented 3 years ago

I haven't used this in years, but I'd say either tweak the header response to get a proper Content-Type or if you can't do it, perhaps parse with response.text() then JSON.parse() manually.

mistricky commented 3 years ago

@BrunoBernardino Thx ur answer, I mock the fetch function by menually, that's works fine for me

bkvishe commented 1 year ago

@mistricky how did you mock it manually?