0

I have a React component which calls an external API. I'm try to unit test this component, but I didn't understand how I can mock the function with jest and "fake" the result.

Component:

import { getWeather } from '../../service/WeatherService'
import {useState, useEffect} from 'react'

export default function Weather() {

...

 useEffect(() => {
        getWeather().then((res) => { // function which calls the external API
...
}

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
arthur-sh
  • 13
  • 1
  • 4

1 Answers1

-1

See https://stackoverflow.com/a/43090261/5798347

You can mock the function like below, which will change the implementation of this function that will be used when you run your tests:

jest.mock('../../service/WeatherService', () => ({
  getWeather: jest.fn() /* Insert Mock Implementation here */
}))
timmeinerzhagen
  • 772
  • 7
  • 10