我有以下 Jest 测试代码来测试 fetch 到 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));
});
});
但我得到:
我知道 movieApiService.getPopularMovies()
是一个 JavaScript 获取请求,但是 Node.js 没有获取 API,那么我如何才能使用 Jest 进行此测试?
最佳答案
我无法使用您提供的代码对此进行测试,但安装并导入 npm 模块 jest-fetch-mock应该可以解决问题。
关于javascript - Jest 类型错误 : fetch is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49618484/