jefflau / jest-fetch-mock

Jest mock for fetch
MIT License
886 stars 117 forks source link

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

Open roni-castro opened 4 years ago

roni-castro commented 4 years ago

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
harish-mohanani commented 2 years ago

Is this possible??