I am trying to create Response Objects for mocking with jest, I can't seem to get the right syntax.
Initialization,
jest.mock('node-fetch') const fetch = require('node-fetch') const { Response, Headers } = jest.requireActual('node-fetch') // Example adapted from https://fetch.spec.whatwg.org/#example-headers-class const meta = { 'Content-Type': 'application/json', 'Accept': '*/*', 'Breaking-Bad': '<3' } // You can in fact use any iterable objects, like a Map or even another Headers const headers = new Headers(meta) const copyOfHeaders = new Headers(headers) const ResponseInit = { status: 200, statusText: 'fail', headers: headers } With a basic test
test('Basic Test', async () => { const token = '' const getDocList = new Response(JSON.stringify(downloadDocumentData), ResponseInit) fetch.mockResolvedValueOnce(Promise.resolve(getDocList)) await module.doSomething('mock', token) .then( async(res) => { await expect(res.data).toEqual(Object) }) }, 5000) I'm getting an error which is
FetchError { message: 'invalid json response body at reason: Unexpected token H in JSON at position 2', type: 'invalid-json' } How can I initial a response for valid json, I have tried a lot of different things.
Following the article at https://jestjs.io/docs/en/bypassing-module-mocks but I want to return and test json instead.