javascript - TypeError: fetch is not a function in React Native & Jest - Stack Overflow

I have a React Native project that contains only the sample Jest tests that are created with a new proj

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).

My project uses fetch for network requests in one of the ponents. 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.

Anyone knows what's going on?

Here's the part of my code that uses 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 />
  );
});

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).

My project uses fetch for network requests in one of the ponents. 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.

Anyone knows what's going on?

Here's the part of my code that uses 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 />
  );
});
Share Improve this question edited Mar 7, 2017 at 13:33 Mihai Damian asked Mar 7, 2017 at 13:01 Mihai DamianMihai Damian 11.4k12 gold badges61 silver badges82 bronze badges 3
  • you should show some code. Also does this help? stackoverflow./questions/30954899/… – Matthias Commented Mar 7, 2017 at 13:12
  • Posted code where I use fetch. I'm not using require for it so my issue seems different. – Mihai Damian Commented Mar 7, 2017 at 13:34
  • fetch es with react-native but not with nodeJS (which is what I guess runs Jest - never used it my self). Try adding a fetch polyfill for you tests – Bruno Grieder Commented Mar 7, 2017 at 13:51
Add a ment  | 

1 Answer 1

Reset to default 8

Add this to your test file.

import 'isomorphic-fetch';

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744262040a4565682.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信