简体   繁体   English

Jest TypeError:fetch 不是函数

[英]Jest TypeError: fetch is not a function

I have the following Jest test code to test a fetch to an endpoint :我有以下 Jest 测试代码来测试对端点提取

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?我知道movieApiService.getPopularMovies()是一个JavaScript fetch request ,但Node.js没有 fetch API,那么我如何使用 Jest 使这个测试工作?

我无法使用您提供的代码对此进行测试,但是安装和导入 npm 模块jest-fetch-mock应该可以解决问题。

Try to keep you mock implementation specific to test cases and if multiple test cases are bound to use the same implementation then wrap them up in a describe block along with a beforeEach call inside it.尽量让你模拟特定于测试用例的实现,如果多个测试用例必须使用相同的实现,那么将它们包装在一个describe块中,并在beforeEach调用beforeEach

This helps in describing mock implementation specific to the scenario being tested.这有助于描述特定于被测试场景的模拟实现。

It is hard to test your implementation with the code you supplied, but let's try switching your mock implementation to something like this:使用您提供的代码很难测试您的实现,但让我们尝试将您的模拟实现切换为这样的:

// This is just dummy data - change its shape in a format that your API renders.
const dummyMoviesData = [
    {title: 'some-tilte-1', body: 'some-1'},
    {title: 'some-tilte-2', body: 'some-2'},
    {title: 'some-tilte-3', body: 'some-3'}
];
global.fetch = jest.fn(() => Promise.resolve(dummyMoviesData));

Now, whenever you movie service API gets called, you may expect the outcome to be of shape of dummyMoviesData and even match it.现在,每当您调用电影服务 API 时,您可能期望结果是 dummyMoviesData 的形状,甚至匹配它。

So,所以,

expect(outcome).toMatchObject(dummyMoviesData);

or

expect(outcome).toEqual(dummyMoviesData);

should do the trick.应该做的伎俩。

As of October 2020, an approach to resolve the error TypeError: fetch is not function is to include the polyfill for fetch using whatwg-fetch .截至 2020 年 10 月,解决错误TypeError: fetch is not function一种方法是使用whatwg-fetch包含用于 fetch 的whatwg-fetch

The package provides a polyfill for .fetch and is well supported and managed by Github.com employees since 2016. It is advisable to read the caveats to understand if whatwg-fetch is the appropriate solution.该软件包提供了一个填充工具.fetch并有良好的支持,并通过Github.com员工,因为2016年是明智的管理阅读注意事项,以了解是否whatwg-fetch是合适的解决方案。

Usage is simple for Babel and es2015+, just add to your file. Babel 和 es2015+ 的使用很简单,只需添加到您的文件中即可。

import 'whatwg-fetch'`

If you are using with Webpack add the package in the entry configuration option before your application entry point.如果您使用 Webpack,请在应用程序入口点之前的入口配置选项中添加包。

entry: ['whatwg-fetch', ...]

You can read the comprehensive documentation at https://github.github.io/fetch/您可以在https://github.github.io/fetch/阅读综合文档

If you are unfamiliar with WhatWG, learn more about the Web Hypertext Application Technology Working Group on their website .如果您不熟悉 WhatWG,请在其网站上了解有关 Web 超文本应用技术工作组的更多信息

安装下面的代码片段并将其添加到我的 jest 文件的顶部为我修复了它:

import "isomorphic-fetch"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM