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

[Question] - Configure the headers globally with json content-type.

Open roni-castro opened this issue 5 years ago • 1 comments

Is it possible to configure the headers globally with the default value as { headers: { 'content-type': 'application/json' }}? Currently everytime I try to do a mockResponseOnce it set the default header as 'Content-Type': [ 'text/plain;charset=UTF-8' ] by default, and I don't want to pass the headers to every mockResponseOnce calls as I don't have requests that return text/plain content:

require('jest-fetch-mock').enableMocks()
...


describe('example', () => {
  beforeEach(() => {
    fetchMock.resetMocks()
  })

  const doRequestFN = async() => {
    const response = await fetch('http://google.com', {})
    console.log(response.headers)
    return response.json()
  }

  test('returns valid values when there are questions answered', async () => {
    fetchMock.mockResponseOnce(JSON.stringify({ data: '12345' }))
    const response = await doRequestFN()
    expect(response).toMatchObject({ data: '12345' })
  })
})

// console.log after executing the request. Captura de Tela 2020-06-24 às 22 45 30

roni-castro avatar Jun 25 '20 01:06 roni-castro

Is this possible??

harish-mohanani avatar Feb 02 '22 20:02 harish-mohanani