jest-fetch-mock
jest-fetch-mock copied to clipboard
Unexpected end of JSON input with CRA
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.
What does your project and setup look like?
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.
Same problem, any workaround here?
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 Thx ur answer, I mock the fetch function by menually, that's works fine for me
@mistricky how did you mock it manually?