简体   繁体   English

TypeError:fetch不是React Native&Jest中的函数

[英]TypeError: fetch is not a function in React Native & Jest

I have a React Native project that contains only the sample Jest tests that are created with a new project (they just check if rendering works on iOS and Android). 我有一个React Native项目,它只包含使用新项目创建的示例Jest测试 (他们只检查渲染是否适用于iOS和Android)。

My project uses fetch for network requests in one of the components. 我的项目使用其中一个组件中的网络请求fetch This works fine when I run the app, but when I run the tests they fail with this error message: 这在我运行应用程序时工作正常,但是当我运行测试时,它们会失败并显示以下错误消息:

TypeError: fetch is not a function. TypeError:fetch不是函数。

Anyone knows what's going on? 谁知道发生了什么?

Here's the part of my code that uses fetch : 这是我的代码中使用fetch

export default class MainScene extends Component {

constructor(props) {
    super(props);
    this.renderRowView = this.renderRowView.bind(this);
}

onFetch(page = 1, callback) {

    var date = new Date();
    date.setDate(date.getDate() - 7);

    fetch(  url, {
        method: 'GET',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }
    })
    .then((response) => response.json())
    .then((responseJson) => {
        if (responseJson.response.pages <= page) {
            callback(responseJson.response.results, {
                allLoaded: true
            });
        } else {
            callback(responseJson.response.results);
        }
    })
    .catch((error) => {
        console.error(error);
    });
}}

An the test file: 测试文件:

import 'react-native';
import React from 'react';
import Index from '../index.ios.js';
import renderer from 'react-test-renderer';

it('renders correctly', () => {
  const tree = renderer.create(
    <Index />
  );
});

将其添加到您的测试文件中。

import 'isomorphic-fetch';

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

相关问题 Jest TypeError:fetch 不是函数 - Jest TypeError: fetch is not a function React Jest - TypeError: (0, _[....]) 不是 function - React Jest - TypeError: (0 , _[....]) is not a function React Native 玩笑不断抛出 -TypeError: Component is not a function error - React Native jest keep throwing -TypeError: Component is not a function error 反应原生TypeError(..)不是函数 - react native TypeError (..) is not a function React Native TypeError:网络请求失败,fetch() - React native TypeError: Network request failed with fetch() TypeError:submitHandler 不是 React Native 上的 function - TypeError : submitHandler is not a function on React Native React Native jest test:TypeError:无法读取未定义的属性&#39;unsubscribeFromTopic&#39; - React Native jest test: TypeError: Cannot read property 'unsubscribeFromTopic' of undefined React native和Jest:我如何执行使用fetch的代码? - React native and Jest: How can I execute code that uses fetch? React/Jest:测试 React Context 方法(结果:TypeError:setScore 不是函数) - React/Jest: Testing React Context methods (Result: TypeError: setScore is not a function) React fetch 多个端点的 API 错误 TypeError: fetch(...).json is not a function - React fetch multiple endpoints of API error TypeError: fetch(...).json is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM