Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest TypeError: fetch is not a function

I have the following Jest test code to test a fetch to an endpoint:

import MovieApiService from 'services/MovieApiService';
import movies from '../constants/movies';

describe('MovieApiService', () => {

  test('if jest work correctly', () => {
    expect(true).toBe(true);
  });

  test('get an array of popular movies', () => {
    global.fetch = jest.mock('../mocks/movies');
    const movieApiService = new MovieApiService();
    return movieApiService.getPopularMovies()
      .then(data => expect(data).toBe(movies));
  });
});

But I am getting:

Enter image description here

I know that the movieApiService.getPopularMovies() is a JavaScript fetch request, but Node.js does not have the fetch API, so how I can I make this test to work using Jest?

like image 497
Jean Avatar asked Apr 02 '18 20:04

Jean


1 Answers

I can't test this with the code you supply, but installing and importing the npm module jest-fetch-mock should do the trick.

like image 104
ajrussellaudio Avatar answered Nov 14 '22 23:11

ajrussellaudio