Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

81
Visualizações
Jest mock for clone function

I have a fetch request and I've never been able to parse the json from the response without cloning the response: response.clone().json() for context:

fetch(url) {
   .then((response) => { 
     response.clone().json()
   })
   .((json) => {
     //updates a state from `useState`
     setResponse(json)
   })

I'm 100% new to testing with Jest and testing-library but so far I've set a mock for the fetch function:

useEffect(() => {
  const mockedData = jest.fn(() => [{test: "title"}]);
  global.fetch = jest.fn().mockResolvedValue((mockedData) =>
  Promise.resolve({
      json: () => Promise.resolve(JSON.stringify(mockedData))
  })
  )
}, []);

The test is failing saying: error fetching response: TypeError: response.clone is not a function.
If I remove the mock for global.fetch I get: ReferenceError: fetch is not defined. So it seems like I need to mock all these functions out?

Short of knowing how I can fix that issue with clone I'd be curious if there is another way I can fetch this data without cloning it.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are correct that you would need to mock all subfunctions, also clone.

But you are also correct that you should not need to clone the data from fetch. Your syntax seems to be a little bit off, and you are not returning the json from your function.

Try this

fetch(url)
    .then((response) => response.json())
    .then((json) => {
        //updates a state from `useState`
        setResponse(json);
    });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda