Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

79
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!