jest-fetch-mock icon indicating copy to clipboard operation
jest-fetch-mock copied to clipboard

Unexpected end of JSON input with CRA

Open mladenp opened this issue 6 years ago • 6 comments

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.

mladenp avatar Apr 22 '18 18:04 mladenp

What does your project and setup look like?

jefflau avatar May 01 '18 04:05 jefflau

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.

BrunoBernardino avatar Sep 10 '18 09:09 BrunoBernardino

Same problem, any workaround here?

mistricky avatar Jun 09 '21 11:06 mistricky

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.

BrunoBernardino avatar Jun 09 '21 11:06 BrunoBernardino

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

mistricky avatar Jun 09 '21 11:06 mistricky

@mistricky how did you mock it manually?

bkvishe avatar Jul 16 '23 19:07 bkvishe