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:
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?
I can't test this with the code you supply, but installing and importing the npm module jest-fetch-mock should do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With