jefflau / jest-fetch-mock

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

Getting ServerParseError on expo and graphql mock #224

Open Davete0302 opened 2 years ago

Davete0302 commented 2 years ago

I'm using react-native with expo, jest-fetch-mock and graphql. When calling the api Im getting error

× return user feed query (69ms)

● testing api › return user feed query

      at _callee$ (graphql/__tests__/api.test.js:22:41)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:63:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:294:22)
  at Generator.next (node_modules/regenerator-runtime/runtime.js:119:21)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:63:40)
  at invoke (node_modules/regenerator-runtime/runtime.js:155:20)

api.test.js import fetch from 'jest-fetch-mock';

      import fetch from 'jest-fetch-mock';

import { getUserFeed } from '../feed'

describe('testing api', () => {
    beforeEach(() => {
        fetch.resetMocks()
    })

    it('return user feed query', async () => {

        //assert on the response
        await getUserFeed(20).then((response) => response.json())
            .then((responseJson) => {
                console.log('res ' + JSON.stringify(responseJson))
            }).catch((error) => {
                console.log('error' + JSON.stringify(error))
            })

        // .then((res) => {
        //     console.log('res '+JSON.stringify(res))
        // }).catch((error) => {
        //     console.log('error'+JSON.stringify(error))
        //     })

        //assert on the times called and arguments given to fetch
        console.log('watch ' + JSON.stringify(fetch.mock.calls))
        expect(fetch.mock.calls.length).toEqual(20)
    })
})

.catch error from console.log

error{"graphQLErrors":[],"clientErrors":[],"networkError":{"name":"ServerParseError","response":{"size":0,"timeout":0},"statusCode":200,"bodyText":""},"message":"Unexpected end of JSON input"}