[英]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.